python sqllite3 的操作

Pyhon 数据操作方法
`

数据库 链接到数据库

def connect(cdb="middb"):
return sqlite3.connect(cdb) #:memory:

数据库 关闭数据库

def close(self):
self.conn.close()

数据库 更改提交到数据库

def commit(self):
self.conn.commit()

数据库 创建表

def create(self, tablename, field):
cur = self.con.cursor()
sql = "CREATE TABLE IF NOT EXISTS %s(%s)" % tablename, field
cur.execute(sql)
self.con.commit()
print("表 %s 创建成功!" % tablename)

数据库 查询表

def select(self, field, tablename, where):
cur = self.con.cursor()
cur.execute("select %s from %s where $s" % field, tablename, where)
return cur.fetchall()

数据库 插入数据

def insert(self, tablename, field, values):
cur = self.con.cursor()
cur.execute("INSERT INTO %s (%s) VALUES (%s)" % tablename, field, values)
self.con.commit()
print("表 %s 新增数据成功!" % tablename)

数据库 修改数据

def update(self, sql):
cur = self.con.cursor()
cur.execute(sql) # "UPDATE COMPANY set SALARY = 25000.00 where ID=1"
self.con.commit()

数据库 删处数据

def delete(self, tablename, where):
cur = self.con.cursor()
cur.execute("DELETE from %s where %s;" % tablename, where)
conn.commit()`

posted @ 2020-11-12 09:23  c_b  阅读(296)  评论(0)    收藏  举报