2017-10-08 28 views
0

我正在爲我的「遊戲」服務器編寫一個安裝程序,並將MySQL用於數據庫。創建數據庫+表格和列時出現錯誤。我在phpmyadmin中測試了Mysql代碼,它工作正常。在c中使用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

回答

2

默認情況下,請求mysql_query c函數不支持多個SQL語句。 但是你可以結合兩個查詢

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

到oneliner

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

注意替換[數據庫]用正確的數據庫名。

解決方法一

使用oneliner

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

謝謝:D祝你有美好的一天。 –

相關問題