时间:2021-07-01 10:21:17 帮助过:16人阅读
增加数据:
conn = sqlite3.connect(board.DatabasePath) conn.row_factory = sqlite3.Row conn.execute("pragma foreign_key=on") c = conn.cursor() md5 = hashlib.md5() md5.update(password) encrypted_passwd = md5.hexdigest() try: #增加数据 c.execute(" insert into register (name,work,email,username,password) values (?,?,?,?,?);", (name, workplace, email, username, encrypted_passwd)) c.execute("select last_insert_rowid() as user_id from register;") except (sqlite3.DatabaseError) as e: print e conn.rollback() return False else: conn.commit() return True finally: conn.close()
删除操作:
conn=sqlite3.connect(board.DatabasePath) conn.execute("pragma foreign_key=on") conn.row_factory=sqlite3.Row c=conn.cursor() try: c.execute("delete from setting where key=? and parent_id=?;",(self.slick.key,self.id)) except (sqlite3.DatabaseError) as e: print e conn.rollback() return False else: conn.commit() self.slick.items.pop(self.id) self=None return True finally: conn.close()
修改操作:
conn=sqlite3.connect(board.DatabasePath) conn.execute("pragma foreign_key=on") conn.row_factory=sqlite3.Row c=conn.cursor() try: c.execute("update setting set value=? where key=? and name=?;",(self.title,self.key,"jumbotron_title")) c.execute("update setting set value=? where key=? and name=?;",(self.content,self.key,"jumbotron_content")) except (sqlite3.DatabaseError) as e: print e conn.rollback() return False else: conn.commit() return True finally: conn.close()
Python连接SQLite数据库
标签:pat cut mit oar none foreign 增加 ash and