Skip to content

Instantly share code, notes, and snippets.

@harshityadav95
Created July 29, 2016 12:25
Show Gist options
  • Save harshityadav95/fcd0cb9a4f4f917a919d1627a9330c89 to your computer and use it in GitHub Desktop.
Save harshityadav95/fcd0cb9a4f4f917a919d1627a9330c89 to your computer and use it in GitHub Desktop.
C# code from Finsar Sqlite to create an Sqlite database and insert some values it no it and display them in a message box
// [snip] - As C# is purely object-oriented the following lines must be put into a class:
// We use these three SQLite objects:
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// ...and inserting another line:
sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// But how do we read something out of our table ?
// First lets build a SQL-Query again:
sqlite_cmd.CommandText = "SELECT * FROM test";
// Now the SQLiteCommand object can give us a DataReader-Object:
sqlite_datareader = sqlite_cmd.ExecuteReader();
// The SQLiteDataReader allows us to run through the result lines:
while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
// MessageBox.Show("Connected Sucessfuly ");
string data = sqlite_datareader.GetString(1);
MessageBox.Show(data);
//System.Console.WriteLine(sqlite_datareader["text"]);
}
// We are ready, now lets cleanup and close our connection:
sqlite_conn.Close();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment