我正在使用 PyQT4 + PostgreSQL 9.1 制作用户管理系统。在编写以下代码以建立与数据库的连接时,出现语法错误:-
from PyQt4 import QtSql, QtGui , QtCore
import _sha256
import psycopg2
def createConnection():
QSqlDatabase db = QSqlDatabase.addDatabase("QPSQL");
db.setDatabaseName("mysite");
db.setUserName("postgres");
db.setPassword("password");
db.setHostName("localhost");
bool ok = db.open();
if not db.open():
QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"),
QtGui.qApp.tr("Unable to establish a database connection.\n"
"This example needs PostgreSQL support."),
QtGui.QMessageBox.Cancel)
return False
query = QtSql.QSqlQuery()
query.exec_("create table usermanagement(ID int primary key,"
"userid int,firstname varchar(20), lastname varchar(20),usertype varchar(15),password varchar(20),)")
query.exec_("insert into usermanagement values(1, 'Danny','Boyle','normaluser','normaluser')")
return True
错误信息如下:-
QSqlDatabase db = QSqlDatabase.addDatabase("QPSQL"); ^ SyntaxError: 无效的语法