本人数据库新手,在创建表时出现问题,最后经查证,找出问题所在。下面的程序是部分节选,在创建数据库表的时候,起先使用的L24的CreateDB,经测试,一直输出 Create testResult Fail, query->isActive()也为false,这就说明问题出在createDB上,也就是说这个QString应该是不符合要求才出错的。后来逐个测试,最后发现是
check varchar(100)的原因,经查看check是数据库关键字。
1 //创建数据库文件路径 2 testResultPath = APPDIR + "/TestReport/"; 3 QDir my_dir(testResultPath); 4 if (!my_dir.exists()) 5 my_dir.mkpath(testResultPath); 6 testResultPath += "testResult.db"; 7 8 //创建数据库 9 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); 10 //db.setHostName("easybook-3313b0"); //可以省略 11 db.setDatabaseName(testResultPath); 12 //db.setUserName("CETG54th"); //设置数据库用户名,可以省略 13 //db.setPassword("123456"); //设置数据库密码,可以省略 14 db.open(); 15 if (!db.isOpen()) 16 { 17 qDebug() << "db is not open"; 18 return; 19 } 20 21 22 //创建数据库表 23 QSqlQuery *query = new QSqlQuery("", db); 24 //const QString createDB = "create table testResult(id int primary key,testDevice varchar(100),project varchar(100),exc varchar(100),display varchar(100),result varchar(100),time varchar(100),check varchar(100))"; //fail 25 26 const QString createDB = "create table testResult(id int primary key,testDevice varchar(100),project varchar(100),exc varchar(100),display varchar(100),result varchar(100),time varchar(100),viewInfo varchar(100))"; //success 27 28 if (query->exec(createDB)) 29 qDebug() << "Create testResult Successful"; 30 else 31 qDebug() << "Create testResult Fail"; 32 33 qDebug() << query->isActive();
下面罗列了数据库关键字
A
ABSOLUTE ACTION ADD ADMINDB
ALL ALLOCATE ALPHANUMERIC ALTER
AND ANY ARE AS
ASC ASSERTION AT AUTHORIZATION
AUTOINCREMENT AVG
B
BAND BEGIN BETWEEN BINARY
BIT BIT_LENGTH BNOT BOR
BOTH BXOR BY BYTE
C
CASCADE CASCADED CASE CAST
CATALOG CHAR CHARACTER CHAR_LENGTH
CHARACTER_LENGTH CHECK CLOSE COALESCE
COLLATE COLLATION COLUMN COMMIT
COMP COMPRESSION CONNECT CONNECTION
CONSTRAINT CONSTRAINTS CONTAINER CONTINUE
CONVERT CORRESPONDING COUNT COUNTER
CREATE CREATEDB CROSS CURRENCY
CURRENT CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP
CURRENT_USER CURSOR
D
DATABASE DATE DATETIME DAY
DEALLOCATE DEC DECIMAL DECLARE
DEFAULT DEFERRABLE DEFERRED DELETE
DESC DESCRIBE DESCRIPTOR DIAGNOSTICS
DISALLOW DISCONNECT DISTINCT DOMAIN
DOUBLE DROP
E
ELSE END END-EXEC ESCAPE
EXCEPT EXCEPTION EXCLUSIVECONNECT EXEC
EXECUTE EXISTS EXTERNAL EXTRACT
F
FALSE FETCH FIRST FLOAT
FLOAT4 FLOAT8 FOR FOREIGN
FOUND FROM FULL
G
GENERAL GET GLOBAL GO
GOTO GRANT GROUP GUID
H
HAVING HOUR
I
IDENTITY IEEEDOUBLE IEEESINGLE IGNORE
IMAGE IMMEDIATE IN INDEX
INDICATOR INHERITABLE INITIALLY INNER
INPUT INSENSITIVE INSERT INT
INTEGER INTEGER1 INTEGER2 INTEGER4
INTERSECT INTERVAL INTO IS
ISOLATION
J
JOIN
K
KEY
L
LANGUAGE LAST LEADING LEFT
LEVEL LIKE LOCAL LOGICAL
LOGICAL1 LONG LONGBINARY LONGCHAR
LONGTEXT LOWER
M
MATCH MAX MEMO MIN
MINUTE MODULE MONEY MONTH
N
NAMES NATIONAL NATURAL NCHAR
NEXT NO NOT NOTE
NULL NULLIF NUMBER NUMERIC
O
OBJECT OCTET_LENGTH OF OLEOBJECT
ON ONLY OPEN OPTION
OR ORDER OUTER OUTPUT
OVERLAPS OWNERACCESS
P
PAD PARAMETERS PARTIAL PASSWORD
PERCENT PIVOT POSITION PRECISION
PREPARE PRESERVE PRIMARY PRIOR
PRIVILEGES PROC PROCEDURE PUBLIC
Q
R
READ REAL REFERENCES RELATIVE
RESTRICT REVOKE RIGHT ROLLBACK
ROWS
S
SCHEMA SCROLL SECOND SECTION
SELECT SELECTSCHEMA SELECTSECURITY SESSION
SESSION_USER SET SHORT SINGLE
SIZE SMALLINT SOME SPACE
SQL SQLCODE SQLERROR SQLSTATE
STRING SUBSTRING SUM SYSTEM_USER
T
TABLE TABLEID TEMPORARY TEXT
THEN TIME TIMESTAMP TIMEZONE_HOUR
TIMEZONE_MINUTE TO TOP TRAILING
TRANSACTION TRANSFORM TRANSLATE TRANSLATION
TRIM TRUE
U
UNION UNIQUE UNIQUEIDENTIFIER UNKNOWN
UPDATE UPDATEIDENTITY UPDATEOWNER UPDATESECURITY
UPPER USAGE USER USING
V
VALUE VALUES VARBINARY VARCHAR
VARYING VIEW
W
WHEN WHENEVER WHERE WITH
WORK WRITE
X
Y
YEAR YESNO
Z
ZONE
上面的关键字主要转自http://zhidao.baidu.com/link?url=RmJBWdmrnxciebfhmXsiJF3bZzgb1Yw0ywkhugHxcyBLKMixClmJl8pulL-m7sasPaAoUgzPqYFlGyPH5gQ36TJOsn7j_iGHDLAbXbfQrJe
另外如果使用数据库出现的问题非上述问题,则推荐博客http://www.cnblogs.com/findumars/p/4207133.html,希望有帮助。