Example of Python connecting to SQLite tutorial

Source: Internet
Author: User
Tags sqlite tutorial
This article through the example code to introduce you to the Python connection SQLite and simple operation, very good, with reference value, need to refer to the friend

Not much nonsense to say, directly to everyone to paste the code, the specific code is as follows:


Import sqlite3# Query def load (table):  #连接数据库  con = sqlite3.connect ("e:/datebase/sqlitestudio/park.db")   # Get cursor  cur = con.cursor ()  #查询整个表  cur.execute (' select *from ' +table)  lists = [' name ', ' password ']  if Table = = ' Login ':    #将数据库列名存入字典    colnames = {desc[0] for desc in cur.description}    stores the dictionary and database data in a list and obtains a record dictionary C11/>rowdicts = [Dict (Zip (lists, row)) for row in Cur.fetchall ()]  else:    rowdicts = [] for    row in cur:      Rowdicts.append (Row)  con.commit ()  cur.close ()  return rowdicts# insert Data def insert_data (Id,name,money):  con = sqlite3.connect ("e:/datebase/sqlitestudio/park.db")  cur = con.cursor ()  #使用SQL语句插入  Cur.execute (' INSERT into Charge values (?,?,?) ', (Id,name, Money))  #插入后进行整表查询 to see if Cur.execute was successfully inserted  (' SELECT * From Charge ')  print (Cur.fetchall ())  con.commit ()  cur.close ()

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.