tags:

views:

381

answers:

3

Hello,

I am still learning quite a bit about programming so this may be an obvious question but I am in the process of writing a program where a user can import an excel file with no column constraints (meaning each excel file could have a different number of columns and so on). What I want to do is be able to convert the imported excel file into a database table using SQLite. As far as I have read the syntax for SQLite is the same as ADO syntax but I am unsure of how to dynamically create the tables in the database. I am looking for any form of suggestions, if someone felt like showing an example in code that would be cool.

Thanks! Nathan

+2  A: 

You can just use ExecuteNonQuery to create your table, using the standard SQLite Query Language for CREATE TABLE.

Reed Copsey
+1  A: 

Here is a piece of code for creating a table in sqlite:

SQLiteConnection mDBcon = new SQLiteConnection();
mDBcon.ConnectionString = "Data Source=" + DataSourcePath;
mDBcon.Open();
SQLiteCommand cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = "CREATE TABLE IF NOT EXISTS tags (ISBN VARCHAR(15), Tag VARCHAR(15));";
cmd.ExecuteNonQuery();

note that in SQLite you can only add coloumn to existing tables, and only to the end of the table:

cmd.CommandText = "ALTER TABLE books ADD COLUMN PublishDate DateTime;";
cmd.ExecuteNonQuery();

Addition

Assuming you imported you data from the excel to a DataSet, you can now iterate through the DataSet's DataTables, create the corresponding tables and fill them: (disclaimer, haven't tested it)

foreach(DataTable table in dataSet.Tables)
{
 SQLiteCommand cmd = new SQLiteCommand(mDBcon);
 cmd.CommandText = "CREATE TABLE IF NOT EXISTS " + table.Name + "(";
 bool first = true;
 foreach (DataColumn column in table.Columns)
 {   
  cmd.CommandText += "@"+column.Name;
  if (!first) cmd.CommandText += ",";
  else first = false;
  cmd.Parameters.Add(new SQLiteParameter("@"+column.Name, column.Name));
 }
 cmd.CommandText += ");";
 cmd.ExecuteNonQuery();

 // Fill the new table:
 SQLiteDataAdapter da = new SQLiteDataAdapter("select * from " + table.Name, mDBcon);
 da.Fill(table);
}
Am
Ah I didn't know you had to already had to have a table made but that's no biggy. I guess my main problem is I can't figure out how to progromattically get the column names from the excel file, get their associated types and then some how turn that into the Create Table statement. If I turn the import into a dataset could I use the dataset schema as my database table schema?
Nathan
@Nathan: haven't used dataset to import data into sqlite, but since it has full ADO support, i assume you can.
Am
Can you think of an example of how I would take column from the dataset and use ADO to add it the Create Table statement?
Nathan
wow, thanks I will take this and work it into my code. I will let you know how it works out tomorrow.
Nathan
A: 

So I have tried out what you suggested and I keep getting this error:

SQLite error near "@TAGID": syntax error

This is what the CommandText looks like : CREATE TABLE IF NOT EXISTS HLMS9126to9128(@TAGID);

This is how I changed the code:

public void CreateSQLiteTable(string tableName) { foreach (DataTable table in LocalDataSet.LocalDs.Tables) { SQLiteCommand cmd = new SQLiteCommand(SQLiteConn.Conn); cmd.CommandText = "CREATE TABLE IF NOT EXISTS " + tableName + "("; bool isfirst = true;

            foreach (DataColumn col in LocalDataSet.LocalDs.Tables[0].Columns)
            {
                cmd.CommandText += "@" + col.ColumnName;

                if (!isfirst) cmd.CommandText += ",";
                else isfirst = false;

                cmd.Parameters.Add(new SQLiteParameter("@" + col.ColumnName, DbType.String, col.ColumnName));
            }
            cmd.CommandText += ");";
            cmd.ExecuteNonQuery();
        }
    }

Any help would be great.

Nathan
Got it figured it out, I need to put [] around my parameter names.
Nathan