ความเห็น: 0
Linux MySQL Programming #3
มาต่อกันนะครับในตัวอย่างนี้เป็นตัวอย่างการเข้าถึง MySQL
และทำการ Update ข้อมูลครับ
// Update1.c
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "user_name", password", "db_test", 0, NULL, 0))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "UPDATE Stock SET ProductPrice = 1234 WHERE ProductID='A01' ");
if (!res)
{
printf("Updated %lu rows\n", (unsigned long)mysql_affected_rows(&my_connection));
} else
{
fprintf(stderr, "Update error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
}
mysql_close(&my_connection);
} else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
Compile โดยใช้คำสั่ง
$ gcc -I/usr/include/mysql update1.c -L/usr/lib/mysql -lmysqlclient -lz -o update1.o
### หากท่านใช้ 64 bit (x86_64) ให้ใช้
$ gcc -I/usr/include/mysql update1.c -L/usr/lib64/mysql -lmysqlclient -lz -o update1.o
ทดสอบการ Run โดย
$ ./update1.o
Connection success
Updated 1 rows
สำเร็จ
สังเกตุว่าไม่ยากเลย เพราะคำสั่ง SQL ก็ไม่ได้แตกต่างกับ การเขียนในภาษาอื่นๆ
บันทึกอื่นๆ
- เก่ากว่า « Linux MySQL Programming #2
- ใหม่กว่า » ใครไม่มี Driver A: เชิญทางนี้ แจ...
ร่วมแสดงความเห็นในหน้านี้