Python + PyQt basic operations, pythonpyqt

Source: Internet
Author: User

Python + PyQt basic operations, pythonpyqt

Sqlite:

Use sqlite3 of Python:

Note that the commit method is slightly different from qt.

import sqlite3class DBManager():    def __init__(self):        self.db = sqlite3.connect("core.db")        self.query = self.db.cursor()          ...        self._initTable()        ...    ## Create table["webpower"] if not exists    def _initTable(self):        self.query.execute("create table if not exists webpower (id integer primary key,name TEXT,state integer)")        self.db.commit()    ...

 

Use QtSql:

 1 from PyQt4.QtSql import QSqlQuery,QSqlDatabase 2  3 class DBManager(): 4     def __init__(self): 5         self.db = QSqlDatabase.addDatabase("QSQLITE") #select database type 6         self.db.setDatabaseName("core.db") # set database name 7         self.db.open()  #connect to or create database   8         self.query = QSqlQuery() #sql handler 9         ...    10     11     ...

 

Mysql:

from PyQt4.QtSql import QSqlQuery,QSqlDatabaseclass DBManager():    def __init__(self):        self.db = QSqlDatabase.addDatabase("QMYSQL")  #select database type        self.db.setHostName("localhost")  #set address        self.db.setUserName("root"); #set user name        self.db.setPassword("root"); #set user pwd        self._trytoConnect() #check connection        self.query = QSqlQuery()        ## create database        self.query.exec_("CREATE DATABASE test1")        ## Choose database        self.db.setDatabaseName("test1")        self._trytoConnect()        #also can be :self.query.exec_("USE test1")    ## Connect and check connection state    def _trytoConnect(self):        if (self.db.open()):                 print "Success"        else:            print "Failed to connect to mysql"    ...

 

Common SQL:

--create tablecreate table if not exists test2 (id integer primary key,time real,powers real);--clear tabledelete from test2;--insertinsert into t1 values(1,0.1,0.2);--selectselect * from test1--delete tabledrop table test4

 

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.