如何在sqlite + juce中创建表和插入数据

时间:2013-10-29 13:35:17

标签: sqlite juce

请告诉我如何在sqlite中创建数据库。你能告诉我如何在sqlite + c ++中创建表格和插入数据。我在网上得不到多少帮助。你能举一些例子吗我能理解。

1 个答案:

答案 0 :(得分: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是一个字符串,包含数据库的路径并使用名称最终确定。 例: String DBPath =“C:\ database.db”;

圣保罗