mysql查询插入不起作用
我试图在mysql数据库中插入一个字段,但它不起作用 我尝试使用“插入到图像”来改变abit,使用查询字符串而不是字符串文字。 我试图弄明白语法是否不同或是什么 我想我是按照计划做的 我的设置 从我得到的mysql查询插入不起作用,mysql,c,Mysql,C,我试图在mysql数据库中插入一个字段,但它不起作用 我尝试使用“插入到图像”来改变abit,使用查询字符串而不是字符串文字。 我试图弄明白语法是否不同或是什么 我想我是按照计划做的 我的设置 从我得到的 i = 1 MySQL Tables in mysql database: images 这是代码 有什么想法吗 /* Simple C program that connects to MySQL Database server*/ #include <mysql.h>
i = 1
MySQL Tables in mysql database:
images
这是代码
有什么想法吗
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
//set the password for mysql server here
char *password = "********";
char *database = "myDB";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables") == 1)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i = mysql_query(conn ,"INSERT INTO `myDB`.`images` (`id`, `date`, `path`) VALUES (NULL, CURRENT_TIMESTAMP, 'mypath/foo/bar')");
printf("i = %d \n", i);
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s \n", row[0]);
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
/*连接MySQL数据库服务器的简单C程序*/
#包括
#包括
#包括
int main(){
MYSQL*康涅狄格州;
MYSQL_RES*RES;
MYSQL_行;
char*server=“localhost”;
char*user=“root”;
//在此处设置mysql服务器的密码
char*password=“**********”;
char*database=“myDB”;
conn=mysql_init(NULL);
if(!mysql\u real\u connect(连接、服务器、用户、密码、数据库、0、NULL、0))
{
fprintf(stderr,“%s\n”,mysql_错误(conn));
出口(1);
}
/*发送SQL查询*/
if(mysql_查询(conn,“show tables”)==1)
{
fprintf(stderr,“%s\n”,mysql_错误(conn));
出口(1);
}
int i=mysql\u查询(conn,“插入到`myDB`.`images`(`id`、`date`、`path`)值(NULL,当前时间戳,`mypath/foo/bar');
printf(“i=%d\n”,i);
res=mysql\u使用结果(conn);
/*输出表名*/
printf(“MySQL数据库中的MySQL表:\n”);
while((row=mysql\u fetch\u row(res))!=NULL)
{
printf(“%s\n”,第[0]行);
}
/*密切联系*/
mysql_free_结果(res);
关闭(康涅狄格州);
返回0;
}
假设id
是主id和INT,它可能是自动递增的。如果是这种情况,请从您的声明中删除id
INSERT INTO `myDB`.`images` (`date`, `path`) VALUES (CURRENT_TIMESTAMP, 'mypath/foo/bar');
如果我们知道表格的格式,回答这个问题会很有帮助。这是假设
date
是时间戳,path
是CHAR或最有可能是VARCHAR。确认您的ID列不是主键,因为主键不允许空列我现在设法让它工作。我找到了一些我试图重新创建的示例,并使其正常工作
它与其他查询不完全相同
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn ,"INSERT INTO images VALUES (NULL , CURRENT_TIMESTAMP , 'awesome')"))
{
fprintf(stderr, "Error: could not execute query \n");
exit(1);
}
int my_bool = mysql_commit(conn);
mysql_free_result(res);
mysql_close(conn);
return 0;
如果只有mysql库具有获取错误消息的函数。顺便说一句,你忘了“;”。必须提交
insert
查询。如果不是,则不执行插入。而且它不会产生任何可以提取的结果。你能举个例子吗@DYZ@DYZ我不知道这一点,但因为我得到了“i=1”作为返回,我认为查询被破坏了。当我读到这里时,它们对1的求值为--“if(status){//cannotexecutestatement}”Yed id是主键。Q中有一个指向该表pic的链接。id是a,因此数据库会自动给它一个数字,因此可以在插入时跳过它。请尝试我提供的语句,看看它是否有效。不,是相同的。我得到的响应I=1,myDB.mysql\u查询中的任何更改都不会返回true或false。也许将其更改为布尔值。用于获取数字响应的更好函数是mysql\u impacted\u rows()。这可能也会让您有所了解。ID是主要的。但我使用AI。该查询是从phpmyadmin提取的。它在那里工作:这更适合作为注释。