Python connects to SQLite database

Source: Internet
Author: User
Tags md5 rollback sqlite sqlite database

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.