Python3 operation SQLite3 creates table primary key auto-increment | CRUD basic operation


Win11 View the installed Python path and installed libraries

Python PEP8 code specification common problems and solutions

Python3 operates MySQL8.XX to create tables | CRUD basic operations

Python3 operation SQLite3 creates table primary key auto-increment | CRUD basic operation

Anaconda3 latest version installation|Usage details|Error: Please select a valid Python interpreter

Examples of integration of Python function drawing and advanced algebra (1): sine function and cosine function

Examples of integration of Python function drawing and advanced algebra (2): Flashpoint function

Example of integration of Python function drawing and advanced algebra (3): setting X|Y axis|grid lines

Example of integration of Python function drawing and advanced algebra (4): Setting the X|Y axis reference line|reference area

Examples of integration of Python function drawing and advanced algebra (5): Comprehensive case of line graphs


1: The primary key grows automatically when creating a table in SQLite3

1: SQLite supports the creation of auto-incrementing primary keys
create table t_user 
(
 id integer primary key autoincrement,
 age int(2), 
 name varchar(10),
 address varchar(100)
)

 2: Union primary key 

 create table t_user
(

 address_id varchar(20), 
 id integer primary key autoincrement, age int(2),
 name varchar(10),
 address varchar(100),

 primary key (id,address_id )

)

Note: When creating a joint primary key, the primary key creation must be placed at the end of all fields, otherwise the creation will fail.

Two: Python uses SQLite3 to create tables

import sqlite3

'''
   Since Python already has built-in SQLite3, you can directly introduce the SQLite3 module to use it.
'''

# Connect to database SQLite
# The database file is data.db. If the file does not exist, it will be automatically created in the current directory.

conn = sqlite3.connect('data.db')
# Create a cursor object Cursor
cursor = conn.cursor()
# Execute a table creation statement to create the t_user table
try:
    cursor.execute("create table t_user (id INTEGER  PRIMARY KEY  AUTOINCREMENT,"
                   " age int(2), name varchar(10),address varchar(100))")
    # submit
    conn.commit()
except Exception as err:
    conn.rollback()
    print("error: ", err)
finally:
    print("Execute finally module: ", "Close system resources")
    # Close cursor
    cursor.close()
    # Close the Connection object
    conn.close()

 Three: Batch insertion of primary key and assignment of primary key of self-increasing data

import sqlite3

'''
  Perform CRUD operations on database SQLite3
'''

conn = sqlite3.connect("data.db")

cursor = conn.cursor()
param = [(None, 20, "Lao Wang", "1-1001 Innovation Center, Zhongguancun Software Park, Haidian District, Beijing"),
         (None, 23, "Lao Li", "1-1011 Innovation Center, Zhongguancun Software Park, Chaoyang District, Beijing"),
         (None, 22, "Lao Gao", "1-1021 Innovation Center, Zhongguancun Software Park, Changping District, Beijing"),
         (None, 25, "Lao Han", "1-1031 Innovation Center, Zhongguancun Software Park, Miyun District, Beijing"),
         (None, 80, "Lao Biao", "1-1041 Zhongguancun Software Park Innovation Center, Fangshan District, Beijing"),
         (None, 90, "老bia", "1-1051 Innovation Center, Zhongguancun Software Park, Fengtai District, Beijing")]
try:
    # Insert data in batches
    sql = 'insert into t_user values(?,?,?,?)'
    cursor.executemany(sql, param)
    conn.commit()
except Exception as err:
    print("Exception when inserting data: ", err)
    conn.rollback()
finally:
    cursor.execute("select * from t_user")
    resultSet = cursor.fetchall()
    for item in resultSet:
        print("Database table (t_user) data set: ", item)

    # Release resources and close cursor
    cursor.close()
    # Close the connection
    conn.close()

Four: View execution results 

D:\program_file_worker\anaconda\python.exe D:\program_file_worker\python_source_work\SSO\grammar\ClassGrammarExecutorCRUD.py 
database table (t_user) data set: (1, 20, 'Lao Wang', 'Zhongguancun Software Park, Haidian District, Beijing Innovation Center 1-1001')
database table (t_user) data set: (2, 23, 'Lao Li', 'Innovation Center 1-1011, Zhongguancun Software Park, Chaoyang District, Beijing') database table (
t_user) data set: (3 , 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center, Changping District, Beijing 1-1021')
database table (t_user) data set: (4, 25, 'Lao Han', 'Zhongguancun Software Park Innovation Center, Miyun District, Beijing Center 1-1031')
database table (t_user) data set: (5, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing') database table (t_user) data set: (6
, 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051')
Database table (t_user) data set: (7, 20, 'Laowang', 'Zhongguancun Software Park Innovation Center, Haidian District, Beijing 1-1001')
Database table (t_user) data set: (8, 23, 'Lao Li', '1-1011, Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing') Database table (t_user) data set: (9,
22 ,'Laogao', 'Zhongguancun Software Park Innovation Center 1-1021, Changping District, Beijing')
database table (t_user) data set: (10, 25, 'Laohan', 'Zhongguancun Software Park Innovation Center 1-1021, Miyun District, Beijing') 1031')
Database table (t_user) data set: (11, 80, 'Laobiao', '1-1041, Zhongguancun Software Park Innovation Center, Fangshan District, Beijing') Database table (t_user) data set: (12, 90, 'Laobiao
' , 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051')
database table (t_user) data set: (13, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center 1-1001, Haidian District, Beijing')
database Table (t_user) data set: (14, 23, 'Lao Li', '1-1011, Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing') Database table (t_user) data set: (15, 22, 'Lao
Gao', 'Zhongguancun Software Park Innovation Center, Changping District, Beijing 1-1021')
database table (t_user) data set: (16, 25, 'Laohan', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing')
database table (t_user) Data set: (17, 80, 'Lao Biao', '1-1041, Zhongguancun Software Park Innovation Center, Fangshan District, Beijing') Database table (
t_user) Data set: (18, 90, 'Lao Biao', ' Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051')
database table (t_user) data set: (19, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center 1-1001, Haidian District, Beijing')
database table (t_user) t_user) data set: (20, 23, 'Lao Li','Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing 1-1011')
database table (t_user) data set: (21, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center 1-1021, Changping District, Beijing')
Database table (t_user) data set: (22, 25, 'Lao Han', '1-1031, Zhongguancun Software Park Innovation Center, Miyun District, Beijing') Database table (t_user) data set: (23, 80, 'Lao Biao
' , 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing')
database table (t_user) data set: (24, 90, 'Laobiao', 'Zhongguancun Software Park Innovation Center 1-1051, Fengtai District, Beijing')

Process finished with exit code 0
 

Five: SQLite3 batch insertion method

Batch insert data into sqlite3 database through Python script
  Two ways to insert data in batches are recorded here:
1. Use execute and loop with for statement
#Import sqlite3 package
import sqlite3
#Create database connection
conn = sqlite3.connect("data.db")
#Get a cursor object
cursor=conn.cursor()
#Set parameter i, for statement loop
for i in range(1,10):
    param=str(i)
    sql="insert into t_user values(?,?,?,?)"
    cursor.execute(sql,param)
    conn.commit()
#Close connection
conn.close()
cursor.close()


2. Through executemany, the data format must be list[tuple(),tuple(),tuple()] or tuple(tuple(),tuple(),tuple())
   import sqlite3
#Open database connection
conn = sqlite3.connect("data.db")
# Use the cursor() method to obtain the operation cursor
cursor = conn.cursor()
#Insert data in batches; mysql uses % sqlite uses?
sql="insert into t_user values(?,?,?,?)"
#Each value is treated as a tuple, and the entire parameter set is treated as a tuple
param=((111111,'haha',13),(22222,'hehe',34))
#Or each value as a tuple, the entire parameter set as a list: param=[(111111,'haha',13),(22222,'hehe',34)]
#Use executemany method to insert data in batches
cursor.executemany(sql,param)
#submit
conn.commit()
#closure
conn.close()
cursor.close()

===============================================================================
3. How cursor executes commands
1. callproc(self, procname, args): used to execute stored procedures. The parameters received are the stored procedure name and parameter list, and the return value is the number of affected rows.
2. execute(self, query, args): Execute a single SQL statement. The parameters received are the SQL statement itself and the parameter list used. The return value is the number of affected rows.
3. executemany(self, query, args): Execute the single sql statement, but repeatedly execute the parameters in the parameter list, and the return value is the number of affected rows
4. nextset(self): Move to the next result set
===============================================================================

4. Methods for cursor to accept return values
1. fetchall(self): Receive all returned result lines.
2. fetchmany(self, size=None): Receive size rows and return result rows. If the value of size is greater than the number of returned result rows, cursor.arraysize data will be returned.
3. fetchone(self): Returns a result row.
4. scroll(self, value, mode='relative'): Move the pointer to a certain row. If mode='relative', it means moving the value bar from the current row. If mode='absolute', it means moving the value bar from the result set. The first row moves the value bar
#Execute query operations
 cursor.execute("select * from cdinfo")
#Use the fetcall method to get all the results returned by the query and save the results to tup. Each result is a tuple type, and all tuples form a tuple set.
 tup=cursor.fetchall()
 print(tup)#Output the entire result
 print tup[0][3]#Output tuple set, the fourth element of the first tuple
 

Six: Three ways to query SQLite3 database in Python

import sqlite3

'''

    Python provides three functions for querying data:
        fetchone(): Get the next record in the query result set
        fetchmany(size) gets the specified number of records
        fetchall(): Get all records in the result set

'''

conn = sqlite3.connect("data.db")
cursor = conn.cursor()
userId = 5
try:
    sql = "select * from t_user"
    cursor.execute(sql)
    # Get query results
    resultOne = cursor.fetchone()
    print("resultOne: ", resultOne)
    resultTwo = cursor.fetchmany(20)
    print("resultTwo: ", resultTwo)
    resultAll = cursor.fetchall()
    print("resultAll: ", resultAll)
    # Use placeholder? to avoid the risk of SQL injection. Tuples are passed as parameters. When there is only one element in the tuple, the last comma cannot be removed.
    resultWhere = cursor.execute("select * from t_user where id <= ?", (userId,))
    print()
    print("resultWhere: ", resultWhere.fetchall())
except Exception as err:
    print("System exception: ", err)

finally:
    cursor.close()
    conn.close()

Seven: Execution results 

D:\program_file_worker\anaconda\python.exe D:\program_file_worker\python_source_work\SSO\grammar\ClassGrammarBatchOperation.py 
resultOne: (1, 20, 'Lao Wang', '1-1001 Zhongguancun Software Park Innovation Center, Haidian District, Beijing' )
resultTwo: [(2, 23, 'Lao Li', 'Zhongguancun Software Park Innovation Center 1-1011, Chaoyang District, Beijing'), (3, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center 1, Changping District, Beijing -1021'), (4, 25, 'Lao Han', 'Zhongguancun Software Park Innovation Center, Miyun District, Beijing 1-1031'), (5, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center, Fangshan District, Beijing Center 1-1041'), (6, 90, 'Laobiao', 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051'), (7, 20, 'Laowang', 'Zhongguancun Software, Haidian District, Beijing Park Innovation Center 1-1001'), (8, 23, 'Lao Li', 'Zhongguancun Software Park Innovation Center 1-1011, Chaoyang District, Beijing'), (9, 22, 'Lao Gao', 'Changping District, Beijing Zhongguancun Software Park Innovation Center 1-1021'), (10, 25, 'Lao Han', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (11, 80, 'Lao Biao', 'Beijing 1-1041, Zhongguancun Software Park Innovation Center, Fangshan District, (12, 90, 'Lao Biao', '1-1051, Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (13, 20, 'Laowang', ' 1-1001, Innovation Center, Zhongguancun Software Park, Haidian District, Beijing), (14, 23, 'Lao Li', '1-1011 Innovation Center, Zhongguancun Software Park, Chaoyang District, Beijing'), (15, 22, 'Lao Gao' , '1-1021, Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (16, 25, 'Laohan','1-1031 Innovation Center, Zhongguancun Software Park, Miyun District, Beijing'), (17, 80, 'Lao Biao', '1-1041 Innovation Center, Zhongguancun Software Park, Fangshan District, Beijing'), (18, 90, 'Lao Biao' ', '1-1051, Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (19, 20, 'Lao Wang', '1-1001, Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (20, 23, ' Lao Li', 'Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing 1-1011'), (21, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center, Changping District, Beijing 1-1021')]
resultAll: [(22, 25, 'Laohan', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (23, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1, Fangshan District, Beijing') -1041'), (24, 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051'), (25, 20, 'Laowang', 'Zhongguancun Software Park Innovation, Haidian District, Beijing Center 1-1001'), (26, 23, 'Lao Li', 'Zhongguancun Software Park Innovation Center 1-1011, Chaoyang District, Beijing'), (27, 22, 'Lao Gao', 'Zhongguancun Software, Changping District, Beijing Park Innovation Center 1-1021'), (28, 25, 'Lao Han', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (29, 80, 'Lao Biao', 'Fangshan District, Beijing Zhongguancun Software Park Innovation Center 1-1041'), (30, 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1051, Fengtai District, Beijing'), (31, 20, 'Lao Wang', 'Beijing 1-1001, Innovation Center, Zhongguancun Software Park, Haidian District'), (32, 23, 'Lao Li', '1-1011 Innovation Center, Zhongguancun Software Park, Chaoyang District, Beijing'), (33, 22, 'Lao Gao', ' 1-1021, Zhongguancun Software Park Innovation Center, Changping District, Beijing), (34, 25, 'Laohan', '1-1031, Zhongguancun Software Park Innovation Center, Miyun District, Beijing'), (35, 80, 'Lao Biao' , '1-1041, Innovation Center, Zhongguancun Software Park, Fangshan District, Beijing'), (36, 90, 'Old Biao','1-1051, Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (37, 20, 'Lao Wang', '1-1001, Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (38, 23, 'Lao Li' ', '1-1011, Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (39, 22, 'Lao Gao', '1-1021, Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (40, 25, ' Lao Han', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (41, 80, 'Lao Biao', '1-1041 Zhongguancun Software Park Innovation Center, Fangshan District, Beijing'), (42, 90 , 'Lao Biao', '1-1051 Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (43, 20, 'Lao Wang', '1-1001 Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (44 , 23, 'Lao Li', '1-1011 Innovation Center, Zhongguancun Software Park, Chaoyang District, Beijing'), (45, 22, 'Lao Gao', '1-1021 Innovation Center, Zhongguancun Software Park, Changping District, Beijing'), (46, 25, 'Lao Han', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (47, 80, 'Lao Biao', '1-1041 Zhongguancun Software Park Innovation Center, Fangshan District, Beijing' ), (48, 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1051, Fengtai District, Beijing'), (49, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center 1-1, Haidian District, Beijing' 1001'), (50, 23, 'Lao Li', '1-1011, Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (51, 22,'Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (52, 25, 'Lao Han', '1-1031 Zhongguancun Software Park Innovation Center, Miyun District, Beijing'), (53, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing'), (54, 90, 'Laobiao', '1-1051 Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), ( 55, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center 1-1001, Haidian District, Beijing'), (56, 23, 'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing') , (57, 22, 'Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (58, 25, 'Lao Han', '1-1031 Zhongguancun Software Park Innovation Center, Miyun District, Beijing '), (59, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing'), (60, 90, 'Old Biao', 'Zhongguancun Software Park Innovation Center 1, Fengtai District, Beijing' -1051'), (61, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center, Haidian District, Beijing 1-1001'), (62, 23, 'Lao Li', 'Zhongguancun Software Park Innovation, Chaoyang District, Beijing Center 1-1011'), (63, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center 1-1021, Changping District, Beijing'), (64, 25, 'Lao Han', 'Zhongguancun Software, Miyun District, Beijing Park Innovation Center 1-1031'), (65, 80, 'Lao Biao','Zhongguancun Software Park Innovation Center, Fangshan District, Beijing 1-1041'), (66, 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051'), (67, 20, 'Laowang' ', 'Zhongguancun Software Park Innovation Center 1-1001, Haidian District, Beijing'), (68, 23, 'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (69, 22, ' Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (70, 25, 'Lao Han', '1-1031 Zhongguancun Software Park Innovation Center, Miyun District, Beijing'), (71, 80 , 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing'), (72, 90, 'Laobiao', '1-1051 Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (73 , 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center 1-1001, Haidian District, Beijing'), (74, 23, 'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (75, 22, 'Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (76, 25, 'Lao Han', '1-1031 Zhongguancun Software Park Innovation Center, Miyun District, Beijing' ), (77, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing'), (78, 90, 'Laobiao', 'Zhongguancun Software Park Innovation Center 1-1, Fengtai District, Beijing') 1051'), (79, 20, 'Lao Wang', '1-1001, Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (80, 23,'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (81, 22, 'Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing'), (82, 25, 'Lao Han', '1-1031 Innovation Center, Zhongguancun Software Park, Miyun District, Beijing'), (83, 80, 'Lao Biao', '1-1041 Innovation Center, Zhongguancun Software Park, Fangshan District, Beijing'), ( 84, 90, 'Lao Biao', '1-1051 Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (85, 20, 'Lao Wang', '1-1001 Zhongguancun Software Park Innovation Center, Haidian District, Beijing') , (86, 23, 'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (87, 22, 'Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing '), (88, 25, 'Laohan', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (89, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1, Fangshan District, Beijing' -1041'), (90, 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051'), (91, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center, Haidian District, Beijing Center 1-1001'), (92, 23, 'Lao Li', 'Zhongguancun Software Park Innovation Center 1-1011, Chaoyang District, Beijing'), (93, 22, 'Lao Gao', 'Zhongguancun Software, Changping District, Beijing Park Innovation Center 1-1021'), (94, 25, 'Laohan','1-1031 Innovation Center, Zhongguancun Software Park, Miyun District, Beijing'), (95, 80, 'Lao Biao', '1-1041 Innovation Center, Zhongguancun Software Park, Fangshan District, Beijing'), (96, 90, 'Lao Biao' ', '1-1051, Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'), (97, 20, 'Lao Wang', '1-1001, Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (98, 23, ' Lao Li', 'Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing 1-1011'), (99, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center, Changping District, Beijing 1-1021'), (100, 25 , 'Lao Han', '1-1031 Innovation Center, Zhongguancun Software Park, Miyun District, Beijing'), (101, 80, 'Lao Biao', '1-1041 Innovation Center, Zhongguancun Software Park, Fangshan District, Beijing'), (102 , 90, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1051, Fengtai District, Beijing'), (103, 20, 'Laowang', '1-1001 Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (104, 23, 'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing'), (105, 22, 'Lao Gao', '1-1021 Zhongguancun Software Park Innovation Center, Changping District, Beijing' ), (106, 25, 'Laohan', 'Zhongguancun Software Park Innovation Center 1-1031, Miyun District, Beijing'), (107, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1, Fangshan District, Beijing') 1041'), (108, 90, '老bia', '1-1051, Zhongguancun Software Park Innovation Center, Fengtai District, Beijing'),(109, 20, 'Lao Wang', '1-1001 Zhongguancun Software Park Innovation Center, Haidian District, Beijing'), (110, 23, 'Lao Li', '1-1011 Zhongguancun Software Park Innovation Center, Chaoyang District, Beijing' ), (111, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center 1-1021, Changping District, Beijing'), (112, 25, 'Lao Han', 'Zhongguancun Software Park Innovation Center 1-1, Miyun District, Beijing' 1031'), (113, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center, Fangshan District, Beijing 1-1041'), (114, 90, 'Old Biao', 'Zhongguancun Software Park Innovation Center, Fengtai District, Beijing 1-1051')]

resultWhere: [(1, 20, 'Lao Wang', 'Zhongguancun Software Park Innovation Center 1-1001, Haidian District, Beijing'), (2, 23, 'Lao Li', 'Zhongguancun Software Park Innovation Center 1, Chaoyang District, Beijing -1011'), (3, 22, 'Lao Gao', 'Zhongguancun Software Park Innovation Center, Changping District, Beijing 1-1021'), (4, 25, 'Lao Han', 'Zhongguancun Software Park Innovation, Miyun District, Beijing Center 1-1031'), (5, 80, 'Lao Biao', 'Zhongguancun Software Park Innovation Center 1-1041, Fangshan District, Beijing')]

Process finished with exit code 0
 

Eight: Update data 

import sqlite3

'''
  Perform update operation on database SQLite3
'''

conn = sqlite3.connect("data.db")

cursor = conn.cursor()

try:
    sql = "update t_user set name=? where id = ?"
    cursor.execute(sql, ('Lao Yang', 1)) #The field with id 1 is updated to "Lao Yang"
    conn.commit()
    print('Execution successful!')
except Exception as err:
    print("An exception occurred during update: ", err)
    conn.rollback()
finally:
    result = cursor.execute("select * from t_user where id = ?", (1,))
    print("The updated data is: ", result.fetchone())
    print("Cursor, database connection closed, release connection resources...")
    cursor.close()
    conn.close()

Nine: Delete operation

import sqlite3

'''
  Perform delete operation on database SQLite3
'''

conn = sqlite3.connect("data.db")

cursor = conn.cursor()
try:
    sql = "delete from t_user where id = ?"
    cursor.execute(sql, (204,)) # id=201 record deletion
    conn.commit()
    print('Execution successful!')
except Exception as err:
    print("An exception occurred during deletion: ", err)
    conn.rollback()
finally:
    result = cursor.execute("select * from t_user where id = 204")
    for item in result.fetchall():
        print("The deleted data is: ", item)

    print("Cursor, database connection closed, release connection resources...")
    cursor.close()
    conn.close()

Guess you like

Origin blog.csdn.net/u014635374/article/details/133303957