locked
got error when i try inserting to sqlite RRS feed

  • Question

  • hi

    i work with sqlite and C# using  SQLite.NET.dll

    i make my table like this:

    string Connect;
            SQLiteConnection cn;
            SQLiteCommand cmd;
            string SQL;
             Connect = @"Data Source=c:\mydb.db;Version=3;New=True;";

     using (cn = new SQLiteConnection(Connect))
               {
                   using (cmd = cn.CreateCommand())
                   {
                       SQL = "CREATE TABLE  MEN(A VARCHAR(20),B VARCHAR(20),C VARCHAR(20),D VARCHAR(20));";
                       cmd.CommandText = SQL;
                       cmd.CommandType = CommandType.Text;
                       cn.Open();
                       cmd.ExecuteNonQuery();
                       cn.Close();
                   }
               }

    i try to insert like this:

     using (cn = new SQLiteConnection(Connect))
                {
                    using (cmd = cn.CreateCommand())
                    {
                        SQL = "insert into MEN(A,B,C,D) values  ('1','2','3','4')";
                        cmd.CommandText = SQL;
                        cmd.CommandType = CommandType.Text;
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();
                    }
                }

    and i got this error:         no such table: MEN

    why ???


    • Edited by E_gold Thursday, July 5, 2012 9:20 PM
    Thursday, July 5, 2012 9:17 PM

Answers

  • Hi,

    first of all: This forum covers transact sql - the sql language of MS SQL Server. So I fear that we do not handle SQLite topics in here.

    But from what I see, you have the parameter New=True inside your connection string. That way, you will always create a new database when opening the database.

    So what is happening:
    You open the conection to create the table:
    - A new database is created
    - The table is created inside the database.

    Now you open the connectin again to insert data:
    - A new database is cerated (so table is gone!)
    - Insert fails because of the new database.

    Please check http://www.connectionstrings.com/sqlite/ to find out about the different options for connection strings.

    With kind regards,

    Konrad

    • Marked as answer by E_gold Thursday, July 5, 2012 9:35 PM
    Thursday, July 5, 2012 9:27 PM

All replies

  • hi

    i work with sqlite and C# using  SQLite.NET.dll

    i make my table like this:

    string Connect;
            SQLiteConnection cn;
            SQLiteCommand cmd;
            string SQL;
             Connect = @"Data Source=c:\mydb.db;Version=3;New=True;";

     using (cn = new SQLiteConnection(Connect))
               {
                   using (cmd = cn.CreateCommand())
                   {
                       SQL = "CREATE TABLE  MEN(A VARCHAR(20),B VARCHAR(20),C VARCHAR(20),D VARCHAR(20));";
                       cmd.CommandText = SQL;
                       cmd.CommandType = CommandType.Text;
                       cn.Open();
                       cmd.ExecuteNonQuery();
                       cn.Close();
                   }
               }

    i try to insert like this:

     using (cn = new SQLiteConnection(Connect))
                {
                    using (cmd = cn.CreateCommand())
                    {
                        SQL = "insert into MEN(A,B,C,D) values  ('1','2','3','4')";
                        cmd.CommandText = SQL;
                        cmd.CommandType = CommandType.Text;
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();
                    }
                }

    and i got this error:         no such table: MEN

    why ???


    • Edited by E_gold Thursday, July 5, 2012 9:20 PM
    • Merged by Chester Hong Wednesday, July 11, 2012 7:41 AM duplicate
    Thursday, July 5, 2012 9:17 PM
  • Hi,

    first of all: This forum covers transact sql - the sql language of MS SQL Server. So I fear that we do not handle SQLite topics in here.

    But from what I see, you have the parameter New=True inside your connection string. That way, you will always create a new database when opening the database.

    So what is happening:
    You open the conection to create the table:
    - A new database is created
    - The table is created inside the database.

    Now you open the connectin again to insert data:
    - A new database is cerated (so table is gone!)
    - Insert fails because of the new database.

    Please check http://www.connectionstrings.com/sqlite/ to find out about the different options for connection strings.

    With kind regards,

    Konrad

    • Marked as answer by E_gold Thursday, July 5, 2012 9:35 PM
    Thursday, July 5, 2012 9:27 PM
  • Double post - also see http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/c49228b7-a7f3-4191-9523-9539210ae6d2
    where i already answered with (please ignore the stuff with wrong forum :) ):

    Hi,

    first of all: This forum covers transact sql - the sql language of MS SQL Server. So I fear that we do not handle SQLite topics in here.

    But from what I see, you have the parameter New=True inside your connection string. That way, you will always create a new database when opening the database.

    So what is happening:
    You open the conection to create the table:
    - A new database is created
    - The table is created inside the database.

    Now you open the connectin again to insert data:
    - A new database is cerated (so table is gone!)
    - Insert fails because of the new database.

    Please check http://www.connectionstrings.com/sqlite/ to find out about the different options for connection strings.

    With kind regards,

    Konrad

    Thursday, July 5, 2012 9:29 PM
  • thanks !!! its work !!
    Thursday, July 5, 2012 9:35 PM