Someone please tell me how can "check if a table exists in sqlite db".
I want to insert logs my db. I want to create a log so I can view it later. I don't want to have all the data in one table. As a result, I wanted to create a separate table for each day but I can't process to query if a table exists.I tried using this line of code but
DateTime time = DateTime.Now;
string logname = time.ToString("MM-dd");
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=data.db3;Version=3;");
m_dbConnection.Open();
string sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=@p1;";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command = m_dbConnection.CreateCommand();
command.Parameters.AddWithValue("@p1", logname);
SQLiteDataReader rdr = command.ExecuteReader();
I always get null value in this line of code even when the table exists
SQLiteDataReader rdr = command.ExecuteReader();
it's my code and i try sql string like this
SELECT name FROM sqlite_master WHERE type='table' AND name=@p1;
SELECT name FROM data WHERE type='table' AND name=@p1;
SELECT name FROM data WHERE type = 'table' AND name = '@p1';
its my code
public static void create_dayend_kk(double ht)
{
string apppath = AppDomain.CurrentDomain.BaseDirectory;
DateTime time = DateTime.Now;
string logname = time.ToString("MM-dd");
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=data.db3;Version=3;");
m_dbConnection.Open();
string sql = "SELECT name FROM data WHERE type = 'table' AND name = '@p1';";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command = m_dbConnection.CreateCommand();
command.Parameters.AddWithValue("@p1", logname);
SQLiteDataReader rdr = command.ExecuteReader();
int intout = rdr.GetInt32(0);
if (intout == 0)
{
sql = "create table @p1 (ID INTEGER, KK REAL, NK REAL, TK REAL,PRIMARY KEY(ID AUTOINCREMENT))";
command = m_dbConnection.CreateCommand();
command.Parameters.AddWithValue("@p1", logname);
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into @p1 (KK, TK) values (@p2, @p3)";
command.Parameters.AddWithValue("@p1", logname);
command.Parameters.AddWithValue("@p2", ht);
command.Parameters.AddWithValue("@p3", ht);
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
else if (intout == 1)
{
sql = "SELECT * FROM @p1";
command = m_dbConnection.CreateCommand();
command.Parameters.AddWithValue("@p1", logname);
rdr = command.ExecuteReader();
double kk = rdr.GetFloat(1);
double nk = rdr.GetFloat(2);
double tk = rdr.GetFloat(3);
tk += ht;
sql = "insert into @p1 (KK, TK) values (@p2, @p3)";
command.Parameters.AddWithValue("@p1", logname);
command.Parameters.AddWithValue("@p2", ht);
command.Parameters.AddWithValue("@p3", tk);
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
m_dbConnection.Close();
}
Please tell me how i can? What’s going wrong?