0

I need to write q query where I check if the table exists, and if it does I need to insert value in there. I'm always getting compile errors cause my syntax is wrong. Cab anyone please point out to me the proper code?Appreciate it

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
        createDataBase();
        createDataTable();
    }

    private void createDataBase()
    {
       SQLiteDataAdapter  dataBase = new SQLiteDataAdapter();
    }


    private SQLiteConnection getConnection() 
    { 
        return new SQLiteConnection("Data Source=file1.db"); 
    }


    private void ExecuteQuery(string txtQuery)
    {            
     using (SQLiteConnection sqlcon =  getConnection())
     {
            using (SQLiteCommand sqlcmd = sqlcon.CreateCommand())
            {
                sqlcmd.CommandText = txtQuery;
                sqlcon.Open();
                sqlcmd.ExecuteNonQuery();
            }
        }
    }


    private void createDataTable()
    {
        //ExecuteQuery("DROP TABLE IF EXISTS 'RECORDS'");
        //ExecuteQuery("CREATE TABLE RECORDS ( ID varchar(255))");
        //ExecuteQuery("CREATE TABLE (IF NOT EXISTS) 'RECORDS'");
        //("IF (EXISTS (SELECT ID FROM sqlite_master WHERE NAME = 'RECORDS'))");
    }


    private void button1_Click(object sender, EventArgs e)
    {
          AddValue(textBox1.Text);  
        ////ExecuteQuery("IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> NULL AND TABLE_NAME = 'RECORDS')) BEGIN INSERT INTO RECORDS (ID) VALUES ('" + textBox1.Text + "')");    
    }


    private void AddValue(string value)
    {
      ExecuteQuery("INSERT INTO RECORDS (ID) VALUES ('" + value + "')");
    }
}

}

4
  • What have you tried? Commented Jun 21, 2012 at 21:45
  • post the compile errors. Commented Jun 21, 2012 at 21:46
  • SQLite error near "RECORDS": syntax error Commented Jun 21, 2012 at 21:48
  • i tried all sorts of versions, can't copy them all here Commented Jun 21, 2012 at 21:50

2 Answers 2

1

This is an example

  SQLiteCommand cmd = new SQLiteCommand();
  cmd.Connection = con;
  cmd.Connection.Open();
  cmd.CommandText = "SELECT name FROM sqlite_master WHERE name='MAIN'";
  SQLiteDataReader rdr = cmd.ExecuteReader();
  if (rdr.HasRows)
  {
        cmd.CommandText = "DROP TABLE 'MAIN'";
        cmd.ExecuteNonQuery();
  }

but this is directly from the SQLite site

  SQLiteCommand cmd = new SQLiteCommand();
  cmd.Connection = con;
  cmd.CommandText = "DROP TABLE IF EXIST 'MAIN'";
  cmd.Connection.Open();
  cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

4 Comments

hi Steve, I inserted exec query because I couldnt understand how to implement your version of it. I now get an error telling that SQLite error no such table: RECORDS, however it would let me compile and run the code, until i insert something in textbox, can you please once again look at my code, i construct a proper version of it?thank you
It's difficult to say where is your problem from the code above. From the error message I suppose that you call the method datatable() and the table "RECORDS" doesn't exist in you SQLite database. However I can't see where your code call the method datatable(). The method ExecuteQuery() is correct, apart from two closing braces missing.
Hi Steve, well that's exactly where I'm having problems. I just can't figure out what is the correct way to construct it. If I choose to run this ExecuteQuery("CREATE TABLE RECORDS ( ID varchar(255))"); >>>>>> it will run only once, and then give me error: RECORDS already exist. If I choose to run this ExecuteQuery("CREATE TABLE (IF NOT EXISTS) 'RECORDS'"); it will give a syntax error, same thing happens with ExecuteQuery("IF (EXISTS (SELECT ID FROM sqlite_master WHERE NAME = 'RECORDS'))");.So, I am completely confused what is the correct way implementing the syntax for this query
Which kind of provider are you using for SQLite? I suppose the System.Data.SQLite. Am i right?
1
IF (EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'TheSchema' 
                 AND  TABLE_NAME = 'TheTable'))
BEGIN
    --your insert logic
END

4 Comments

Sorry I haven't tested on SQLite. I hope it will because the given sample is in ANSI.
I tries to follow this pattern, but I always get compile errors.
Not sure why it is failing in your case. Following example is fully functional in my case where I have a table called Person. Please post your actual SQL for more support. IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> NULL AND TABLE_NAME = 'Person')) BEGIN select * from Person END
Hi s_nair! as far as I understand implementing query this way, would require putting it in the button_click method, since it will create table and insert item subsequently. I've tried to do that, but I failed to accomplish anything.I inserted my best version of it, in the code, I would appreciate If you look it up and correct it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.