Python3 Connect to the SQLite instance, directly on the code bar
Import sqlite3db = r "D:\pyWork\test.db" #pyWork目录下test. db database File Drp_tb_sql = "DROP table if exists staff" Crt_tb_sql = " "" CREATE table if not EXISTS staff ( ID integer primary key autoincrement unique NOT NULL, name varchar (), C ity varchar (100)); "" #连接数据库con = sqlite3.connect (db) cur = con.cursor () #创建表staffcur. Execute (drp_tb_sql) Cur.execute (crt_tb_sql) # Insert Record Insert_sql = "INSERT into staff (name,city) VALUES (?,?)" For placeholder Cur.execute (insert_sql, (' Tom ', ' New York ') Cur.execute (Insert_sql, (' Frank ', ' Los Angeles ') Cur.execute ( Insert_sql, (' Kate ', ' Chicago ')) Cur.execute (Insert_sql, (' Thomas ', ' Houston ')) Cur.execute (Insert_sql, (' Sam ', ' Philadelphia ') con.commit () #查询记录select_sql = "SELECT * FROM Staff" Cur.execute (select_sql) #返回一个list, The object type in list is tuple (tuple) Date_set = Cur.fetchall () for row in Date_set: print (Row) cur.close () Con.close ()