SQLite使用C ++插入语句 [英] SQLite Insert Statement with C++

查看:123
本文介绍了SQLite使用C ++插入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在test2.cpp上尝试过的代码

Below is the code I have tried on a test2.cpp

我检查了我的数据库,发现没有添加新的记录。

I checked my database and found out no new record had been added. What actually went wrong in my statement?

#include <iostream>
#include <sqlite3.h>

//g++ -o test2 test2.cpp -lsqlite3
using namespace std;

int main()
{
int counter = 0;

    sqlite3 *db;
    sqlite3_stmt * stmt;

string username = "panda";
string name = "Kungfu Panda";
string department = "normal";
string password = "hellopassword";

string sqlstatement = "INSERT INTO abe_account (" + username + "," + name + "," + department + "," + password + ");";

    if (sqlite3_open("abeserver.db", &db) == SQLITE_OK)
    {
    sqlite3_prepare( db, sqlstatement.c_str(), -1, &stmt, NULL );//preparing the statement
    sqlite3_step( stmt );//executing the statement
        }
    else
    {
        cout << "Failed to open db\n";
    }

    sqlite3_finalize(stmt);
    sqlite3_close(db);


    return 0;

}

我想询问是否可以知道语句成功执行。就像添加一行,从sqlite3的某种形式的确认。

I would like to ask if it's possible to know if the statement is executed with success too. Like one row added, some form of confirmation from sqlite3. And if there's an error, will it be able to cout out too?

推荐答案

当你做一个插入时,你通常指定字段按照您要提供数据的顺序。否则,您必须以正确的顺序指定所有数据(按照定义的顺序为表中的所有字段传递一个值)。

When you do an insert, you generally specify the fields in the order that you are going to supply the data. Otherwise you have to specify all data in the correct order (passing a value for all fields in the table in the order it was defined).

因此您的语法不完整。 ..或者这样做:

Your syntax is therefore incomplete... Either do this:

INSERT INTO tablename (field1, field2, field3) VALUES (value1, value2, value3);

或:

INSERT INTO tablename VALUES (value1, value2, value3)

没有绑定数据到一个准备好的查询,你需要引用你的字符串。在 panda 中替换用户名是不正确的。您需要提供'panda'。因此,字符串像这样:

Also, because you are not binding data to a prepared query, you need to quote your strings. It's not okay to just substitute in panda for the username. You need to supply 'panda'. Therefore, the strings go in like this:

"('" + username + "','" + name + "','" + department + "','" + password + "');"

因为很容易弄乱这个问题(并且有特殊字符的转义码, 单引号),您可能希望使用一个函数来引用至少的字符串:

Because it's easy to mess this up (and there are escape codes for special characters, eg the single-quote), you might prefer to make a function to quote the string which at the very least would do:

string quotesql( const string& s ) {
    return string("'") + s + string("'");
}

然后:

"(" + quotesql(username) + "," + quotesql(name) + ...

所以,你可能最终会得到这个(假设字段名):

So, all up you might end up with this (assuming field names):

string sqlstatement =
    "INSERT INTO abe_account (username, name, department, password) VALUES ("
    + quotesql(username) + ","
    + quotesql(name) + ","
    + quotesql(department) + ","
    + quotesql(password) + ");";

这篇关于SQLite使用C ++插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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