Advertisement
BloodknightStudios

sqlite.cs

Jan 29th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. // SQLite integration script
  2.  
  3. function sqliteTest(%dbname)
  4. {
  5.    %sqlite = new SQLiteObject(sqlite);
  6.    if (%sqlite == 0)
  7.    {
  8.       echo("ERROR: Failed to create SQLiteObject. sqliteTest aborted.");
  9.       return;
  10.    }
  11.    
  12.    // open database
  13.    if (sqlite.openDatabase(%dbname) == 0)
  14.    {
  15.       echo("ERROR: Failed to open database: " @ %dbname);
  16.       echo("       Ensure that the disk is not full or write protected.  sqliteTest aborted.");
  17.       sqlite.delete();
  18.       return;
  19.    }
  20.    
  21.    // create a new simple table for demonstration purposes
  22.    %query = "CREATE TABLE users (username VARCHAR(20))";
  23.    %result = sqlite.query(%query, 0);
  24.    if (%result == 0)
  25.    {
  26.       // query failed
  27.       echo("ERROR: Failed to create new table.  sqliteTest aborted.");
  28.       sqlite.closeDatabase();
  29.       sqlite.delete();
  30.       return;
  31.    }
  32.    // result sets are persistant, and you can have more than one at any time.  This
  33.    // allows for cross referencing data or what not.  However, each result set takes up
  34.    // memory and large queries will take up a significant amount of memory.
  35.    // If you no longer need a result set, you should clear it.
  36.    sqlite.clearResult(%result);
  37.    // insert some data into the table
  38.    %query = "INSERT INTO users (username) VALUES ('john')";
  39.    %result = sqlite.query(%query, 0);
  40.    if (%result == 0)
  41.    {
  42.       // query failed
  43.       echo("ERROR: Failed to INSERT test data into table.  Attempting to DROP table before aborting.");
  44.       if (sqlite.query("DROP TABLE users", 0) == 0)
  45.          echo("Failed to DROP table.  sqliteTest aborted.");
  46.       else
  47.          echo("Table DROP completed properly.  sqliteTest aborted.");
  48.       sqlite.closeDatabase();
  49.       sqlite.delete();
  50.       return;
  51.    }
  52.    sqlite.clearResult(%result);
  53.    // retrieve some data from the table
  54.    %query = "SELECT * FROM users";
  55.    %result = sqlite.query(%query, 0);
  56.    if (%result == 0)
  57.    {
  58.       echo("ERROR: Failed to SELECT from users table.");
  59.    }
  60.    // attempt to retrieve result data
  61.    while (!sqlite.endOfResult(%result))
  62.    {
  63.       %username = sqlite.getColumn(%result, "username");
  64.       echo("Retrived username = " @ %username);
  65.       sqlite.nextRow(%result);
  66.    }
  67.    
  68.    sqlite.clearResult(%result);
  69.    // delete the table
  70.    %result = sqlite.query("DROP TABLE users", 0);
  71.    if (%result == 0)
  72.    {
  73.       echo("ERROR: Failed to DROP table");
  74.    }
  75.    //sqlite.clearResult(%result);
  76.    // close database
  77.    sqlite.closeDatabase();
  78.    
  79.    // delete SQLite object.
  80.    sqlite.delete();
  81. }
  82.  
  83. function sqlite::onQueryFailed(%this, %error)
  84. {
  85.    echo ("SQLite Query Error: " @ %error);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement