你能告诉我如何在sqlite中创建数据库吗?你能告诉我如何在sqlite +c ++中创建表和插入数据吗?我在互联网上没有得到太多帮助。你能举个例子吗?能够理解。
问问题
555 次
1 回答
0
这是我的做法:
char *sql;
int rc = 0;
File *fptr_db = new File(DBPath);
if(fptr_db->existsAsFile())
{
} else {
sqlite3_initialize( );
rc = sqlite3_open_v2( DBPath.toRawUTF8(), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
if ( rc != SQLITE_OK) {
sqlite3_close( pDb );
exit( -1 );
}
sql = "CREATE TABLE IF NOT EXISTS Users (Id integer PRIMARY KEY NOT NULL, GroupId integer NOT NULL, Name VARCHAR(40) NOT NULL COLLATE NOCASE, Password VARCHAR(200) COLLATE NOCASE, Address VARCHAR(200) COLLATE NOCASE, PostalCode VARCHAR(20), Vat VARCHAR(30), BI INT, LastSale DATE, Limits DECIMAL(12,6), Commission DECIMAL(12,6), BirthDate DATE, Phone VARCHAR(20), MobilePhone VARCHAR(20), Email VARCHAR(200) COLLATE NOCASE, Obs VARCHAR, DefaultSerieId integer NOT NULL ); ";
rc = sqlite3_prepare_v2( pDb, sql, -1, &query, NULL );
if ( rc != SQLITE_OK) exit( -1 );
rc = sqlite3_step( query );
if ( rc != SQLITE_DONE ) exit ( -1 );
sql = "INSERT INTO [Users] ([Id], [GroupId], [Name], [Password], [DefaultSerieId]) VALUES (1, 0, 'Paulo', '1234', 0)";
rc = sqlite3_prepare_v2( pDb, sql, -1, &query, NULL );
if ( rc != SQLITE_OK) exit( -1 );
rc = sqlite3_step( query );
if ( rc != SQLITE_DONE ) exit ( -1 );
sql = "INSERT INTO [Users] ([Id], [GroupId], [Name], [Password], [DefaultSerieId]) VALUES (2, 0, 'Antonio', '', 0)";
rc = sqlite3_prepare_v2(pDb, sql, -1, &query, NULL);
if (rc != SQLITE_OK) exit(-1);
rc = sqlite3_step(query);
if (rc != SQLITE_DONE) exit(-1);
sqlite3_finalize( query );
sqlite3_close( pDb );
sqlite3_shutdown( );
}
效果很好,DBPath 是一个字符串,其中包含数据库的路径并以名称完成。示例:字符串 DBPath = "C:\database.db";
保罗
于 2014-01-11T09:23:32.500 回答