Share Python3 Connect SQLite instance code

Source: Internet
Author: User
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 ()
Related Article

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.