Get the App
SLTechnology News&Howtos  ›  Database  › 

How to connect to sqlite database in PyQt4

Shulou Source: shulou.com Published: 2022-05-31 19:33:59 09月23日 Update

This article is to share with you about how to connect to the sqlite database in PyQt4, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

SQLite, is a lightweight database, is an ACID-compliant relational database management system, its design goal is embedded, and it has been used in many embedded products, it occupies very low resources, in embedded devices, it may only need a few hundred kilograms of memory. The following is a brief description of how pyqt connects to sqlite database:

#-*-coding: utf-8-*-from PyQt4.QtGui import * from PyQt4.QtCore import * from PyQt4.QtSql import * import sys # create database connection def createConnection (): # Select the database type, here create the database test0.db for the sqlite3 database db=QSqlDatabase.addDatabase ("QSQLITE") # and open it if it exists Otherwise, create the database db.setDatabaseName ("test0.db") # Open the database db.open () # create the table def createTable (): # create a QsqlQuery object to execute the sql statement q=QSqlQuery () q.exec_ ("create table if not exists T1 (F1 integer primary key,f2 varchar (20)") q.exec1 ("delete from T1") # here u is used to convert the string into unicode encoding Solve Chinese garbled Q.execu "insert into T1 values") q.exececu "insert into T1 values") q.execc _ ("commit") class Model (QSqlTableModel): def _ init__ (self,parent): QSqlTableModel.__init__ (self) Parent) # set the table name to load self.setTable ("T1") # this step should be to execute the query Do not quite understand the strategy of self.select () # data update For more information, please see the Qt document self.setEditStrategy (QSqlTableModel.OnManualSubmit) class TestWidget (QWidget): def _ _ init__ (self): QWidget.__init__ (self) vbox=QVBoxLayout (self) self.view=QTableView () self.model=Model (self.view) self.view.setModel (self.model) vbox.addWidget (self.view) if _ name__== "_ _ main_ _ ": a=QApplication (sys.argv) createConnection () createTable () w=TestWidget () w.show () sys.exit (a.execec) above is how to connect to the sqlite database in PyQt4 The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Data database embedded more knowledge articles practical garbled products memory characters strings objects that is work meetings documents articles methods goals see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft vpn Huawei Redmi