Home  >  Article  >  Backend Development  >  Example tutorial of connecting python to sqlite

Example tutorial of connecting python to sqlite

零下一度
零下一度Original
2017-07-02 10:46:131694browse

This article introduces you to python connectionsqlite and simple operations through example code. It is very good and has reference value. Friends who need it can refer to it

Without further ado, I will post the code directly for you. The specific code is as follows:


import sqlite3
#查询
def load(table):
  #连接数据库
  con = sqlite3.connect("E:/Datebase/SQLiteStudio/Park.db")
   #获得游标
  cur = con.cursor()
  #查询整个表
  cur.execute('select *from '+table)
  lists = ['name','password']
  if table == 'login':
    #将数据库列名存入字典
    colnames = {desc[0] for desc in cur.description}
    将字典和数据库的数据一起存入列表,获得了记录字典
    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
#插入数据
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))
  #插入后进行整表查询,看是否成功插入
  cur.execute('select *from Charge')
  print(cur.fetchall())
  con.commit()
  cur.close()

The above is the detailed content of Example tutorial of connecting python to sqlite. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn