Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a_samp>
- #include <sqlitei>
- main() {}
- public OnGameModeInit() {
- new
- DB:db,
- DBStatement:stmt,
- int_value,
- Float:float_value,
- string_value[128],
- DBResult:result
- ;
- db = db_open("temp_test.db");
- // Create a temporary table and fill it with a bunch of crap
- db_query(db, "CREATE TEMPORARY TABLE test (float REAL, integer INTEGER, string TEXT)");
- db_query(db, "INSERT INTO temp.test VALUES( 1247.5789, 0, 'lorem ')");
- db_query(db, "INSERT INTO temp.test VALUES( 435.8624, 11111, 'ipsum ')");
- db_query(db, "INSERT INTO temp.test VALUES( 3245.32546, 22222, 'dolor ')");
- db_query(db, "INSERT INTO temp.test VALUES( 541.7653, 33333, 'natoque ')");
- db_query(db, "INSERT INTO temp.test VALUES( 3444.43214 44444, 'penatibus')");
- db_query(db, "INSERT INTO temp.test VALUES( 6223.9857, 55555, 'et ')");
- db_query(db, "INSERT INTO temp.test VALUES( 1.1234, 66666, 'magnis ')");
- db_query(db, "INSERT INTO temp.test VALUES( 5421.6532, 77777, 'dis ')");
- db_query(db, "INSERT INTO temp.test VALUES(53122.8475, 88888, 'parturien')");
- db_query(db, "INSERT INTO temp.test VALUES( 643.1543, 99999, 'montes ')");
- // ====================================================================================================
- // Print out the table we just created
- // ====================================================================================================
- result = db_query(db, "SELECT * FROM temp.test");
- db_print_result(result);
- db_autofree_result(result);
- // ====================================================================================================
- // Let's add two rows!
- // ====================================================================================================
- // Prepare a statement
- stmt = db_prepare(db, "INSERT INTO temp.test VALUES(?, ?, ?)");
- // Set the values for the questionmarks (first one is 0, second is 1, etc.)
- stmt_bind_value(stmt, 0, DB::TYPE_FLOAT , 999.9999);
- stmt_bind_value(stmt, 1, DB::TYPE_INT , 133337);
- stmt_bind_value(stmt, 2, DB::TYPE_STRING, "there's no need to \"escape\" anything!");
- // Run the statement
- stmt_execute(stmt);
- // Change a parameter (questionmark)
- stmt_bind_value(stmt, 0, DB::TYPE_FLOAT , 512.45678);
- stmt_bind_value(stmt, 2, DB::TYPE_STRING, "\\/ changed string and float!!!!! \\/");
- // Run the statement again
- stmt_execute(stmt);
- // Close the statement (free it from the memory).
- // The variable "stmt" will also have its value set to DB::INVALID_STATEMENT.
- stmt_close(stmt);
- // ====================================================================================================
- // Let's loop through the table. Pay attention now..
- // ====================================================================================================
- // Prepare a new statement
- stmt = db_prepare(db, "SELECT * FROM temp.test WHERE integer >= ?");
- // Set the value for the first questionmark
- stmt_bind_value(stmt, 0, DB::TYPE_INT, 0);
- // Bind the results. Whenever a row is fetched, the variables passed to stmt_bind_result_field
- // will have their values set to the respective field (by its index).
- stmt_bind_result_field(stmt, 0, DB::TYPE_FLOAT , float_value);
- stmt_bind_result_field(stmt, 1, DB::TYPE_INT , int_value);
- // Strings require an additional argument containing the max length
- stmt_bind_result_field(stmt, 2, DB::TYPE_STRING, string_value, sizeof(string_value));
- // Execute the statement!
- if (stmt_execute(stmt)) {
- // When the last row is fetched, the result will be freed.
- while (stmt_fetch_row(stmt))
- printf("float: %.2f; integer: %d; string: %s", float_value, int_value, string_value);
- }
- print(" ");
- // ====================================================================================================
- // Wasn't that fun? Let's only get the rows with "integer" larger than 50000.
- // ====================================================================================================
- stmt_bind_value(stmt, 0, DB::TYPE_INT, 50000);
- if (stmt_execute(stmt)) {
- while (stmt_fetch_row(stmt))
- printf("float: %.2f; integer: %d; string: %s", float_value, int_value, string_value);
- }
- // Close the statement
- stmt_close(stmt);
- // ..and the database.
- db_close(db);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement