如果我们必须使用c api将记录插入到mysql数据库中,则通用代码是这样
if (mysql_query(conn, "insert into empinfo values ('saikat banerjee')"))
{
printf("4Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
但是,在这里我们必须从代码本身输入记录。
但是,我想从直接用户输入中输入记录。
例如,将用户输入保存在一个“说”字符串变量中,然后使用该字符串变量将记录存储在数据库中。即。我想做这样的事情
char empname[100]
接着,
if (mysql_query(conn, "insert into empinfo values (empname)"))
{
printf("4Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
但是,这会将
NULL
存储在表中。有什么办法吗?
请帮忙!
最佳答案
Mysql C(或其他方式)的API不会进入-stdio.h会进入。我建议查看一下scanf和fread的文档。使用libreadline的其他要点(如果在Linux上)。当然,mysql(CLI命令)已经做到了所有这些以及更多。
但是,您的问题的第二部分使我认为您的需求更加深入,看到您至少需要执行以下操作:
sprintf(stmt, "insert into empinfo values ('%s')",empname)
if (mysql_query(conn, stmt)) ...
不过,这是关于普通的旧C语言的。
关于mysql - 在mysql c api中从用户获取输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8806941/