SQLite as a lightweight database, there are many management tools, such as SQLite Expert Professional, is very suitable for storing Python web site, crawler data, the following list of basic additions and deletions to change the operation
Read operation:
CONN1 =Sqlite3.connect (board. DatabasePath) Conn1.row_factory=Sqlite3. Rowconn1.execute ("pragma foreign_key=on") C1=conn1.cursor ()Try:#perform a query operationC1.execute ("Select Username as username from register where name=?;", (username,))#Exception Handlingexcept(sqlite3. Databaseerror) as E:PrintereturnNoneElse: User_row=C1.fetchone ()#Call Datafinally: Conn1.close ()#Close Connection
Add Data:
conn =Sqlite3.connect (board. DatabasePath) Conn.row_factory=Sqlite3. Rowconn.execute ("pragma foreign_key=on") C=conn.cursor () MD5=hashlib.md5 () md5.update (password) encrypted_passwd=md5.hexdigest ()Try:#Add DataC.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:Printe conn.rollback ()returnFalseElse: Conn.commit ()returnTruefinally: Conn.close ()
Delete operation:
conn=Sqlite3.connect (board. DatabasePath) Conn.execute ("pragma foreign_key=on") Conn.row_factory=Sqlite3. ROWC=conn.cursor ()Try: C.execute ("Delete from setting where key=? and parent_id=?;", (self.slick.key,self.id))except(sqlite3. Databaseerror) as E:Printe conn.rollback ()returnFalseElse: Conn.commit () Self.slick.items.pop (self.id) self=NonereturnTruefinally: Conn.close ()
Modify the operation:
conn=Sqlite3.connect (board. DatabasePath) Conn.execute ("pragma foreign_key=on") Conn.row_factory=Sqlite3. ROWC=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:Printe conn.rollback ()returnFalseElse: Conn.commit ()returnTruefinally: Conn.close ()
Python connects to SQLite database