Sqlite和langauge C使用scanf插入

时间:2016-12-25 22:23:57

标签: c sqlite

我搜索了如何将用户提供的信息插入到数据库中 我在这个论坛上找到了很多方法,但是他们没有工作。你可以给一门课程或者tutoriel怎么做?

感谢您的帮助

PS:我找到的方法 Methode 1:

printf("\nEnter NameID of broker whose priority has to be changed and the new priority");
                 scanf("%s%d",nameID,&priority);
                 sql = " UPDATE BROKERLIST set PRIORITY = '"+priority+"' where NAMEID = '"+nameID+"'; ";
                 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
                 if( rc != SQLITE_OK ){
                        fprintf(stderr, "SQL error: %s\n", zErrMsg);
                        sqlite3_free(zErrMsg);

Methode 2:

char sql[] =
            "INSERT INTO STUDENTS VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
    sqlite3_stmt *stmt = NULL;
    int retval = sqlite3_prepare_v2(handle, sql, -1, &stmt, NULL);
    if (retval != SQLITE_OK) {
        printf("Error while creating insert statement. '%s'",
                sqlite3_errmsg(handle));
        return;
    }
    sqlite3_bind_null(stmt, 1);
    sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 3, surname, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 4, street_no);
    sqlite3_bind_text(stmt, 5, street_name, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 6, suburb, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 7, postcode);
    sqlite3_bind_int(stmt, 8, student_number);
    sqlite3_bind_text(stmt, 9, dob, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 10, gender, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 11, course, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 12, status, -1, SQLITE_STATIC);
    sqlite3_bind_double(stmt, 13, wam);

Methode 3

string query = "INSERT INTO login (name,username,password) VALUES ('" + username + "','" + name + "','" + password + "');";

如果您可以提供简单的方法,那将是您的好事,因为我没有在学校学习sqlite但是我需要它用于项目使用C管理银行

1 个答案:

答案 0 :(得分:1)

创建表格

$ sqlite3 test.db
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table t (xyz text);
sqlite> .quit

您可以使用的程序如下。

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#define CALL_SQLITE(f)                                          \
    {                                                           \
        int i;                                                  \
        i = sqlite3_ ## f;                                      \
        if (i != SQLITE_OK) {                                   \
            fprintf (stderr, "%s failed with status %d: %s\n",  \
                     #f, i, sqlite3_errmsg (db));               \
            exit (1);                                           \
        }                                                       \
    }                                                           \

#define CALL_SQLITE_EXPECT(f,x)                                 \
    {                                                           \
        int i;                                                  \
        i = sqlite3_ ## f;                                      \
        if (i != SQLITE_ ## x) {                                \
            fprintf (stderr, "%s failed with status %d: %s\n",  \
                     #f, i, sqlite3_errmsg (db));               \
            exit (1);                                           \
        }                                                       \
    }

int main ()
{
    sqlite3 * db;
    char * sql;
    sqlite3_stmt * stmt;
    char str[256];
    printf("Please input word to store in sqlite? ");
    fgets(str, sizeof(str), stdin);
    CALL_SQLITE (open ("test.db", & db));
    sql = "INSERT INTO t (xyz) VALUES (?)";
    CALL_SQLITE (prepare_v2 (db, sql, strlen (sql) + 1, & stmt, NULL));
    CALL_SQLITE (bind_text (stmt, 1, str, 6, SQLITE_STATIC));
    CALL_SQLITE_EXPECT (step (stmt), DONE);
    printf ("row id was %d\n", (int) sqlite3_last_insert_rowid (db));
    return 0;
}

使用-lsqlite3

进行编译
   gcc -Wall main.c -lsqlite3

输入单词&#34; apple&#34;并将其存储在表中如下所示:

$ ./a.out
Please input word to store in sqlite? apple
row id was 4
$ sqlite3 test.db 
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> select * from t;
fruit
fruit
foo

apple