python connects sqlite and simple operation

  • 2020-06-07 04:50:46
  • OfStack

Without further ado, I posted the code directly to you. The specific code is as follows:


import sqlite3
# The query 
def load(table):
  # Connect to database 
  con = sqlite3.connect("E:/Datebase/SQLiteStudio/Park.db")
   # To obtain the cursor 
  cur = con.cursor()
  # Query the entire table 
  cur.execute('select *from '+table)
  lists = ['name','password']
  if table == 'login':
    # Store the database column names in the dictionary 
    colnames = {desc[0] for desc in cur.description}
     Combine dictionary and database data 1 I've got a dictionary of records 
    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()
  # use SQL Statements insert 
  cur.execute('insert into Charge values (?,?,?)', (ID,name, money))
  # After the insertion, the whole table is queried to see whether the insertion was successful 
  cur.execute('select *from Charge')
  print(cur.fetchall())
  con.commit()
  cur.close()

Related articles: