gpt4 book ai didi

c - Sqlite 和 C 语言使用 scanf 插入

转载 作者:行者123 更新时间:2023-11-30 21:00:26 25 4
gpt4 key购买 nike

我搜索了如何将用户提供的信息插入数据库我找了很多方法但是都没有效果。我该怎么做?

PS:我找到的方法

方法一:

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);

方法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);

方法3

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

如果你能给出简单的方法来做到这一点,那就太好了,因为我在学校没有学习sqlite,但我需要它来使用C进行银行项目管理

最佳答案

创建表

$ 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

输入单词“apple”并将其存储在表中的用户如下所示:

$ ./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

关于c - Sqlite 和 C 语言使用 scanf 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41324942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com