在Linux編程中,Curd(創(chuàng)建、讀取、更新、刪除)操作是數(shù)據(jù)庫交互的基礎(chǔ)。以下是一個使用C語言和SQLite數(shù)據(jù)庫進行CRUD操作的簡單示例,遵循了一些最佳實踐:
sqlite3_open
函數(shù)連接到SQLite數(shù)據(jù)庫。確保檢查返回值以處理可能的錯誤。#include <sqlite3.h>
int main() {
sqlite3 *db;
char *errorMessage = 0;
int connection;
connection = sqlite3_open("test.db", &db);
if (connection) {
fprintf(stderr, "無法打開數(shù)據(jù)庫: %s\n", sqlite3_errmsg(db));
return 0;
}
}
sqlite3_exec
函數(shù)執(zhí)行SQL命令。這個函數(shù)可以執(zhí)行任何有效的SQL語句,但通常建議使用預(yù)處理語句以提高安全性。char *sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL);";
connection = sqlite3_exec(db, sql, 0, 0, &errorMessage);
if (connection != SQLITE_OK) {
fprintf(stderr, "SQL錯誤: %s\n", errorMessage);
sqlite3_free(errorMessage);
}
sqlite3_prepare_v2
函數(shù)準備SQL語句,并使用sqlite3_bind_*
函數(shù)綁定參數(shù)。這有助于防止SQL注入攻擊。char *sql = "INSERT INTO users (name, email) VALUES (?, ?);";
sqlite3_stmt *statement;
connection = sqlite3_prepare_v2(db, sql, -1, &statement, 0);
if (connection != SQLITE_OK) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
return 0;
}
sqlite3_bind_text(statement, 1, "John Doe", -1, SQLITE_STATIC);
sqlite3_bind_text(statement, 2, "john.doe@example.com", -1, SQLITE_STATIC);
connection = sqlite3_step(statement);
if (connection != SQLITE_DONE) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
sqlite3_prepare_v2
和sqlite3_step
函數(shù)執(zhí)行SELECT查詢,并使用sqlite3_column_*
函數(shù)獲取結(jié)果。sql = "SELECT * FROM users;";
statement = 0;
connection = sqlite3_prepare_v2(db, sql, -1, &statement, 0);
if (connection != SQLITE_OK) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
return 0;
}
while ((connection = sqlite3_step(statement)) == SQLITE_ROW) {
int id = sqlite3_column_int(statement, 0);
const char *name = sqlite3_column_text(statement, 1);
const char *email = sqlite3_column_text(statement, 2);
printf("ID: %d, Name: %s, Email: %s\n", id, name, email);
}
if (connection != SQLITE_DONE) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
sqlite3_prepare_v2
、sqlite3_bind_*
和sqlite3_step
函數(shù)執(zhí)行UPDATE語句。sql = "UPDATE users SET email = ? WHERE id = ?;";
statement = 0;
connection = sqlite3_prepare_v2(db, sql, -1, &statement, 0);
if (connection != SQLITE_OK) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
return 0;
}
sqlite3_bind_text(statement, 1, "new.email@example.com", -1, SQLITE_STATIC);
sqlite3_bind_int(statement, 2, 1);
connection = sqlite3_step(statement);
if (connection != SQLITE_DONE) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
sqlite3_prepare_v2
、sqlite3_bind_*
和sqlite3_step
函數(shù)執(zhí)行DELETE語句。sql = "DELETE FROM users WHERE id = ?;";
statement = 0;
connection = sqlite3_prepare_v2(db, sql, -1, &statement, 0);
if (connection != SQLITE_OK) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
return 0;
}
sqlite3_bind_int(statement, 1, 1);
connection = sqlite3_step(statement);
if (connection != SQLITE_DONE) {
fprintf(stderr, "SQL錯誤: %s\n", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
sqlite3_close
函數(shù)關(guān)閉數(shù)據(jù)庫連接。sqlite3_close(db);
這個示例展示了如何在Linux編程中使用C語言和SQLite數(shù)據(jù)庫執(zhí)行基本的CRUD操作,同時遵循了一些最佳實踐,如錯誤處理、參數(shù)綁定和使用預(yù)處理語句。在實際應(yīng)用中,你可能需要根據(jù)具體需求調(diào)整代碼,例如添加更多的錯誤檢查、使用連接池管理數(shù)據(jù)庫連接等。此外,對于生產(chǎn)環(huán)境,建議使用更高級的數(shù)據(jù)庫管理系統(tǒng),如PostgreSQL或MySQL,并提供相應(yīng)的驅(qū)動程序和庫。