user2788405
user2788405

Reputation: 325

How to populate SQlite columns with list values in c#

Background:

Noob here - Anyways, I'm building a c# forms app which parses through data in text files. I've stored textile data in a list.

Problem:

I don't know how to populate an sqlite table with this data. How do I transfer my list data into columns in my sqlite table?

SQlite Code: (Following is taken from finisar)

     private void button4_Click(object sender, EventArgs e)
    {

        // [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:
            string data = sqlite_datareader.GetString(1);
            MessageBox.Show(data);
        }

        // We are ready, now lets cleanup and close our connection:
        sqlite_conn.Close();
    }

So this is where I want to transfer the list data to my sqlite table: sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";

I don't know where exactly, or how to implement the foreach loop syntax here.

Thanks

Upvotes: 0

Views: 2006

Answers (1)

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11161

From your code above, you may notice following section is repeated:

    sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (..., ...);";
    sqlite_cmd.ExecuteNonQuery();

So, it's here you must use a cycle.

Depending on how you read text, you must use appropriate cycle.

This is an example using TextReader:

using (StreamReader reader = File.OpenText("somefile.txt")) {
    //Use a transaction: you should do bulk inserts inside transactions
    DbTransaction tr = sqlite_conn.BeginTransaction();
    //Create a command once.
    DbCommand cmd = sqlite_conn.CreateCommand();
    //Assign SQL with parameters (?)
    cmd.CommandText = "INSERT INTO test (id, text) VALUES (?, ?);"
    string line;
    int line_num;
    //Read all lines from file
    for (line_num=0; (line = reader.ReadLine()) != null; ++line_num) {
        //For each line, assign correct parameters
        cmd.Parameters(0).Value=line_num;
        cmd.Parameters(1).Value=line;
        //Send command to database
        cmd.ExecuteNonQuery();
    }
    //Commit - only at this point data is written to database
    tr.Commit();
}

Upvotes: 1

Related Questions