1. Makefile
INCLUDE=/usr/include/mysql/
LIBS=/usr/lib/mysql
all:
gcc -I$(INCLUDE) -L$(LIBS) -lmysqlclient xxx.c -o xxx
clean:
rm -f xxx
2. mysql的頭文件和庫文件配置
如果mysql是默認安裝的,在Makefile裡配置
INCLUDE=/usr/include/mysql
LIBS=/usr/lib/mysql
如果mysql是安裝到/usr/local/mysql,在Makefile裡配置
INCLUDE=/usr/local/mysql/include/mysql
LIBS=/usr/local/mysql/lib/mysql
如果mysql在/usr/local/mysql目錄,還需要添加一條鏈接,否則可能出現 “libmysqlclient.so.16: cannot open shared object file: No such file or directory”
ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib/libmysqlclient.so.16
3. Unknown column 'opcode' in 'where clause'
檢查sql語句是否寫的有誤。
4. Commands out of sync; you can't run this command now
如果你在你的客戶代碼中得到Commands out of sync; You can't run this command now,你正在以錯誤的次序調用客戶函數
我在代碼裡為了獲取結果的行數,使用如下語句報的這個錯誤
mysql_real_query(mysql, sql, strlen(sql));
num = mysql_affected_rows(mysql);
後來我把語句改成如下就可以了
mysql_real_query(mysql, sql, strlen(sql));
rs = mysql_store_result(mysql);
num = mysql_num_rows(rs);
5. Mysql將查詢結果放到另一張表裡
首先得建好另一張表,然後
insert into new_table select xxx,xxx,xxx from old_table where xxx;
6. 取查詢結果
MYSQL *mysql;
MYSQL_RES *rs;
MYSQL_ROW row;
char *sql = "select uid, uname from users where uid=xxx";
mysql_real_connect(mysql, sql, strlen(sql);
rs = mysql_store_result(mysql);
row = mysql_fetch_row(rs);
結果就存放在row中,row[0],row[1]分別是uid,uname,可以使用
atoi或者atol將結果轉化成整數。
7. 運行時出現“浮點數例外”www.2cto.com
出現這個是因為代碼中有除0,檢查代碼對除數為0進行判斷。