📜  sqlite save db (1)

📅  最后修改于: 2023-12-03 15:35:07.332000             🧑  作者: Mango

SQLite Save DB

SQLite is a popular relational database management system that is widely used by programmers. It is used to store and manage data using a SQL-based query language. With SQLite, developers can create, read, update, and delete data from a database file without having to run a separate server process.

One of the key advantages of SQLite is that it is embedded in most operating systems, which means that developers can use it without any additional installation or setup. SQLite is also lightweight, fast, and reliable, which makes it an attractive option for many types of applications.

Saving a database in SQLite

Saving data in SQLite is relatively simple. To save data, developers can use the sqlite3_open function to open a database connection and the sqlite3_exec function to execute SQL statements. For example, the following code opens a database connection, creates a table, and inserts some data into it:

sqlite3 *db;
char *errMsg = 0;

/* Open database */
if (sqlite3_open("mydb.sqlite", &db) != SQLITE_OK) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 1;
}

/* Create table */
char *sql = "CREATE TABLE mytable (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);";
if (sqlite3_exec(db, sql, NULL, 0, &errMsg) != SQLITE_OK) {
    fprintf(stderr, "SQL error: %s\n", errMsg);
    sqlite3_free(errMsg);
}

/* Insert data */
sql = "INSERT INTO mytable (id, name, age) VALUES (1, 'Bob', 30);"
if (sqlite3_exec(db, sql, NULL, 0, &errMsg) != SQLITE_OK) {
   fprintf(stderr, "SQL error: %s\n", errMsg);
   sqlite3_free(errMsg);
}

/* Close database */
sqlite3_close(db);

The code above creates a database file named mydb.sqlite, opens a connection to it with sqlite3_open, creates a table called mytable with three columns (id, name, and age) using a SQL CREATE TABLE statement, and inserts a row of data into mytable using a SQL INSERT INTO statement.

Retrieving data from a database in SQLite

Retrieving data from a database is also simple in SQLite. Developers can use the sqlite3_prepare_v2 function to prepare a SQL statement, the sqlite3_step function to execute it, and the sqlite3_column_* functions to retrieve the results. For example, the following code retrieves the data we inserted in the previous example:

sqlite3_stmt *stmt;

/* Open database */
if (sqlite3_open("mydb.sqlite", &db) != SQLITE_OK) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 1;
}

/* Prepare statement */
char *sql = "SELECT id, name, age FROM mytable WHERE id = 1;";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
    fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 1;
}

/* Execute statement */
if (sqlite3_step(stmt) == SQLITE_ROW) {
    /* Retrieve data */
    int id = sqlite3_column_int(stmt, 0);
    const unsigned char *name = sqlite3_column_text(stmt, 1);
    int age = sqlite3_column_int(stmt, 2);

    /* Print data */
    printf("id = %d, name = %s, age = %d\n", id, name, age);
}

/* Finalize statement */
sqlite3_finalize(stmt);

/* Close database */
sqlite3_close(db);

The code above opens the mydb.sqlite file, prepares a SQL SELECT statement to retrieve the row with id equal to 1, executes the statement using sqlite3_step, retrieves the results using the sqlite3_column_* functions, and prints them to the console.

Conclusion

SQLite is a powerful and flexible database management system that is easy to use and integrate into your application. With SQLite, you can store and manage data with ease, and retrieve it using SQL statements. Whether you are building a simple application or a complex system, SQLite is an excellent choice for managing your data.