Advertisement
Guest User

SQLitei Demo

a guest
Dec 14th, 2011
2,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.44 KB | None | 0 0
  1. #include <a_samp>
  2. #include <sqlitei>
  3.  
  4. main() {}
  5.  
  6. public OnGameModeInit() {
  7.     new
  8.                  DB:db,
  9.         DBStatement:stmt,
  10.                     int_value,
  11.               Float:float_value,
  12.                     string_value[128],
  13.            DBResult:result
  14.     ;
  15.    
  16.     db = db_open("temp_test.db");
  17.    
  18.     // Create a temporary table and fill it with a bunch of crap
  19.     db_query(db, "CREATE TEMPORARY TABLE test (float REAL, integer INTEGER, string TEXT)");
  20.     db_query(db, "INSERT INTO temp.test VALUES( 1247.5789,               0, 'lorem    ')");
  21.     db_query(db, "INSERT INTO temp.test VALUES(  435.8624,           11111, 'ipsum    ')");
  22.     db_query(db, "INSERT INTO temp.test VALUES( 3245.32546,          22222, 'dolor    ')");
  23.     db_query(db, "INSERT INTO temp.test VALUES(  541.7653,           33333, 'natoque  ')");
  24.     db_query(db, "INSERT INTO temp.test VALUES( 3444.43214           44444, 'penatibus')");
  25.     db_query(db, "INSERT INTO temp.test VALUES( 6223.9857,           55555, 'et       ')");
  26.     db_query(db, "INSERT INTO temp.test VALUES(    1.1234,           66666, 'magnis   ')");
  27.     db_query(db, "INSERT INTO temp.test VALUES( 5421.6532,           77777, 'dis      ')");
  28.     db_query(db, "INSERT INTO temp.test VALUES(53122.8475,           88888, 'parturien')");
  29.     db_query(db, "INSERT INTO temp.test VALUES(  643.1543,           99999, 'montes   ')");
  30.    
  31.     // ====================================================================================================
  32.     // Print out the table we just created
  33.     // ====================================================================================================
  34.    
  35.     result = db_query(db, "SELECT * FROM temp.test");
  36.    
  37.     db_print_result(result);
  38.    
  39.     db_autofree_result(result);
  40.    
  41.     // ====================================================================================================
  42.     // Let's add two rows!
  43.     // ====================================================================================================
  44.    
  45.     // Prepare a statement
  46.     stmt = db_prepare(db, "INSERT INTO temp.test VALUES(?, ?, ?)");
  47.    
  48.     // Set the values for the questionmarks (first one is 0, second is 1, etc.)
  49.     stmt_bind_value(stmt, 0, DB::TYPE_FLOAT , 999.9999);
  50.     stmt_bind_value(stmt, 1, DB::TYPE_INT   , 133337);
  51.     stmt_bind_value(stmt, 2, DB::TYPE_STRING, "there's no need to \"escape\" anything!");
  52.    
  53.     // Run the statement
  54.     stmt_execute(stmt);
  55.    
  56.     // Change a parameter (questionmark)
  57.     stmt_bind_value(stmt, 0, DB::TYPE_FLOAT , 512.45678);
  58.     stmt_bind_value(stmt, 2, DB::TYPE_STRING, "\\/ changed string and float!!!!! \\/");
  59.    
  60.     // Run the statement again
  61.     stmt_execute(stmt);
  62.    
  63.     // Close the statement (free it from the memory).
  64.     // The variable "stmt" will also have its value set to DB::INVALID_STATEMENT.
  65.     stmt_close(stmt);
  66.    
  67.     // ====================================================================================================
  68.     // Let's loop through the table. Pay attention now..
  69.     // ====================================================================================================
  70.    
  71.     // Prepare a new statement
  72.     stmt = db_prepare(db, "SELECT * FROM temp.test WHERE integer >= ?");
  73.    
  74.     // Set the value for the first questionmark
  75.     stmt_bind_value(stmt, 0, DB::TYPE_INT, 0);
  76.    
  77.     // Bind the results. Whenever a row is fetched, the variables passed to stmt_bind_result_field
  78.     // will have their values set to the respective field (by its index).
  79.     stmt_bind_result_field(stmt, 0, DB::TYPE_FLOAT , float_value);
  80.     stmt_bind_result_field(stmt, 1, DB::TYPE_INT   , int_value);
  81.    
  82.     // Strings require an additional argument containing the max length
  83.     stmt_bind_result_field(stmt, 2, DB::TYPE_STRING, string_value, sizeof(string_value));
  84.    
  85.     // Execute the statement!
  86.     if (stmt_execute(stmt)) {
  87.         // When the last row is fetched, the result will be freed.
  88.         while (stmt_fetch_row(stmt))
  89.             printf("float: %.2f; integer: %d; string: %s", float_value, int_value, string_value);
  90.     }
  91.    
  92.     print(" ");
  93.    
  94.     // ====================================================================================================
  95.     // Wasn't that fun? Let's only get the rows with "integer" larger than 50000.
  96.     // ====================================================================================================
  97.    
  98.     stmt_bind_value(stmt, 0, DB::TYPE_INT, 50000);
  99.    
  100.     if (stmt_execute(stmt)) {
  101.         while (stmt_fetch_row(stmt))
  102.             printf("float: %.2f; integer: %d; string: %s", float_value, int_value, string_value);
  103.     }
  104.    
  105.     // Close the statement
  106.     stmt_close(stmt);
  107.    
  108.     // ..and the database.
  109.     db_close(db);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement