This article mainly introduces how to connect to the SQLite database using Python3, which is widely used in Python database programming, for more information about how to connect Python 3 to the SQLite database, see the example in this article. Share it with you for your reference. The specific method is as follows:
The instance code is as follows:
Import sqlitedb = r "D: \ pyWork \ test. db "# test in the pyWork directory. 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 (100 ), city varchar (100); "# connect to the database con = sqlite3.connect (db) cur = export cute (crt_tb_ SQL) # insert record insert_ SQL =" insert into staff (name, city) Values (?,?) "#? The parameter is set to cur.exe cute (insert_ SQL, ('Tom ', 'new York' using cur.exe cute (insert_ SQL, ('frank', 'Los into cute (insert_ SQL, ('Sam', 'philadelphia ')) con. commit () # query record select_ SQL = "select * from staff" cur.exe cute (select_ SQL) # returns a list. The object type in the list is tuple (tuples) date_set = cur. fetchall () for row in date_set: print (row) cur. close () con. close ()
I hope this example will help you learn Python.