c ++:使用SQLite创建数据库插入&更新 [英] c++ : Create database using SQLite for Insert & update

查看:282
本文介绍了c ++:使用SQLite创建数据库插入&更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用sqlite3 lib创建一个数据库在c + +。我得到错误 sqlite3_prepare_v2'

没有在此范围
中声明,如logcat所示。



日志文件

  .. \src\Test.cpp:在函数'int main(int,const char **)'中:
..\src\Test.cpp:21:85:错误:'sqlite3_prepare_v2'未在此范围中声明
。 .\src\Test.cpp:30:13:错误:变量'sqlite3在'有初始化器但不完整的类型
..\src\Test.cpp:30:30:错误:无效使用不完整类型'sqlite3 {aka struct sqlite3}'
.. \src\ / sqlite3.h:73:16:错误:正向声明'sqlite3 {aka struct sqlite3}'
  

#include< iostream>
using namespace std;
#includesqlite3.h

int main(int argc,const char * argv []){

sqlite3 * db;
sqlite3_open(test.db,& db);

string createQuery =CREATE TABLE IF NOT EXISTS items(busid INTEGER PRIMARY KEY,ipaddr TEXT,time TEXT NOT NULL DEFAULT(NOW()));;
sqlite3_stmt * createStmt;
cout<< 创建表语句< endl;
sqlite3_prepare_v2(db,createQuery.c_str(),createQuery.size(),& createStmt,NULL);
cout<< 步进表语句< endl;
if(sqlite3_step(createStmt)!= SQLITE_DONE)cout<< 没有创建表! << endl;

string insertQuery =INSERT INTO items(time,ipaddr)VALUES('test','192.168.1.1');; // WORKS!


sqlite3_stmt * insertStmt;
cout<< 创建插入语句< endl;
sqlite3_prepare(db,insertQuery.c_str(),insertQuery.size(),& insertStmt,NULL);
cout<< Stepping Insert Statement< endl;
if(sqlite3_step(insertStmt)!= SQLITE_DONE)cout<< 没有插入项目! << endl;

cout<< 成功! << endl;

return 0;
}

请帮助我。感谢.....

解决方案

  #include< sqlite3.h> 

应包含sqlite3_prepare_v2和struct sqlite3。确保你包含正确的sqlite3.h文件。



在sqlite3_prepare_v2中,第三个参数可以是(你应该是这样)-1,所以sql是



使用sqlite 3.7.11工作裸机示例:

  #include< sqlite3.h> 
int test()
{
sqlite3 * pDb = NULL;
sqlite3_stmt * query = NULL;
int ret = 0;
do //避免嵌套if的
{
//初始化引擎
if(SQLITE_OK!=(ret = sqlite3_initialize()))
{
printf (无法初始化库:%d \\\
,ret);
break;
}
//打开到DB的连接
if(SQLITE_OK!=(ret = sqlite3_open_v2(test.db,& pDb,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,NULL)))
{
printf(无法打开conn:%d \\\
,ret);
break;
}
//准备语句
if(SQLITE_OK!=(ret = sqlite3_prepare_v2(pDb,SELECT 2012,-1,& query,NULL)))
{
printf(无法准备插入:%d,%s\\\
,ret,sqlite3_errmsg(pDb));
break;
}
//到第一行数据
if(SQLITE_ROW!=(ret = sqlite3_step(query)))//参见文档,这可以返回更多的值as success
{
printf(步骤失败:%d,%s\\\
,ret,sqlite3_errmsg(pDb));
break;
}
// ...并打印列0的值(expect 2012 here)
printf(sqlite:%s,sqlite3_column_text(query,0)

} while(假);
// cleanup
if(NULL!= query)sqlite3_finalize(query);
if(NULL!= pDb)sqlite3_close(pDb);
sqlite3_shutdown();
return ret;
}

希望这有助于


I am trying to create a database in c++ using sqlite3 lib.. I am getting error sqlite3_prepare_v2'
was not declared in this scope
as shown in logcat.

log file

..\src\Test.cpp: In function 'int main(int, const char**)':
..\src\Test.cpp:21:85: error: 'sqlite3_prepare_v2' was not declared in this scope
..\src\Test.cpp:30:13: error: variable 'sqlite3 in' has initializer but incomplete type
..\src\Test.cpp:30:30: error: invalid use of incomplete type 'sqlite3 {aka struct sqlite3}'
..\src\/sqlite3.h:73:16: error: forward declaration of 'sqlite3 {aka struct sqlite3}'

Here is my code

#include <iostream>
using namespace std;
 #include "sqlite3.h"

 int main (int argc, const char * argv[]) {

sqlite3 *db;
sqlite3_open("test.db", & db);

   string createQuery = "CREATE TABLE IF NOT EXISTS items (busid INTEGER PRIMARY KEY, ipaddr TEXT,    time TEXT NOT NULL DEFAULT (NOW()));";
   sqlite3_stmt *createStmt;
  cout << "Creating Table Statement" << endl;
  sqlite3_prepare_v2(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL);
   cout << "Stepping Table Statement" << endl;
   if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl;

   string insertQuery = "INSERT INTO items (time, ipaddr) VALUES ('test', '192.168.1.1');"; // WORKS!


   sqlite3_stmt *insertStmt;
   cout << "Creating Insert Statement" << endl;
   sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL);
   cout << "Stepping Insert Statement" << endl;
   if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl;

cout << "Success!" << endl;

 return 0;
}

please help me out. thanks.....

解决方案

 #include <sqlite3.h>

should contain sqlite3_prepare_v2 and struct sqlite3. Make sure you're including the right sqlite3.h file.

Also in sqlite3_prepare_v2 the 3rd arg can be (and should be in your case) -1 so the sql is read to the first null terminator.

Working bare-metal sample using sqlite 3.7.11:

#include <sqlite3.h>
int test()
{
    sqlite3* pDb = NULL;
    sqlite3_stmt* query = NULL;
    int ret = 0;
    do // avoid nested if's
    {
        // initialize engine
        if (SQLITE_OK != (ret = sqlite3_initialize()))
        {
            printf("Failed to initialize library: %d\n", ret);
            break;
        }
        // open connection to a DB
        if (SQLITE_OK != (ret = sqlite3_open_v2("test.db", &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)))
        {
            printf("Failed to open conn: %d\n", ret);
            break;
        }
        // prepare the statement
        if (SQLITE_OK != (ret = sqlite3_prepare_v2(pDb, "SELECT 2012", -1, &query, NULL)))
        {
            printf("Failed to prepare insert: %d, %s\n", ret, sqlite3_errmsg(pDb));
            break;
        }
        // step to 1st row of data
        if (SQLITE_ROW != (ret = sqlite3_step(query))) // see documentation, this can return more values as success
        {
            printf("Failed to step: %d, %s\n", ret, sqlite3_errmsg(pDb));
            break;
        }
        // ... and print the value of column 0 (expect 2012 here)
        printf("Value from sqlite: %s", sqlite3_column_text(query, 0));     

    } while (false);
    // cleanup
    if (NULL != query) sqlite3_finalize(query);
    if (NULL != pDb) sqlite3_close(pDb);
    sqlite3_shutdown();
    return ret;
}

Hope this helps

这篇关于c ++:使用SQLite创建数据库插入&amp;更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆