gpt4 book ai didi

mysql - 在c中使用mysql时出错

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:17 24 4
gpt4 key购买 nike

我正在为我的“游戏”服务器编写设置程序,我使用 MySQL 作为数据库。创建数据库 + 表和列时出现错误。我在 phpmyadmin 中测试了 Mysql 代码并且它有效。

这是我的代码示例:`

int area;
char user[100];
char pass[100];
char ip[200];
char tentativaspm;
void mysqlsetup(){


MYSQL *CON = mysql_init(NULL);
if (CON == NULL)
{
fprintf(stderr, "%s\n", mysql_error(CON));
exit(1);

}
if (mysql_real_connect(CON, ip, user, pass,
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(CON));
mysql_close(CON);
sleep(2);
userepassmysql();
}
printf("Conecao establecida.\n");
if(mysql_query(CON, "CREATE DATABASE place;")){
fprintf(stderr, "%s\n", mysql_error(CON));
mysql_close(CON);
exit(1);


}


if(mysql_query(CON, "USE place; CREATE TABLE grid (pixelID int,color int);"){
fprintf(stderr, "%s\n", mysql_error(CON));
mysql_close(CON);
exit(1);

}

我得到这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USE grid CREATE TABLE grid (pixelID int(11) NOT NULL,color int(11) NOT N' at line 1

最佳答案

默认情况下,mysql_query c 函数不支持多个 SQL 语句。但是你可以结合这两个查询

USE [database];
CREATE TABLE grid (pixelID int,color int)

进入单类机

CREATE TABLE [database].grid (pixelID INT, color INT) 

请注意将 [database] 替换为正确的数据库名称。

解决方案一

使用单行本

if(mysql_query(CON, "CREATE TABLE place.grid (pixelID int,color int);"){

方案二

使用mysql_set_server_option()函数启用多条SQL语句。

mysql_set_server_option(connection, MYSQL_OPTION_MULTI_STATEMENTS_ON);

关于mysql - 在c中使用mysql时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46630657/

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