How to connect to the SQLite database using Python3

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.