SQLite: create table and add a row if the table doesn't exist

Sebastian

I have a problem with a query in SQLite. I can't achieve my goal because I get a syntax error. I'm trying to check if table accounts exists and if not exists create it and add a row. I've tried several codes, and the last one was this:

IF NOT EXISTS (SELECT * FROM account) THEN
   BEGIN
       CREATE TABLE account (rowID INT, user VARCHAR(20), pass VARCHAR(20))
       INSERT INTO account (rowID, user, pass) VALUES (0, '', '')
   END

I'm using SQLite 3 with C#

I can get it to work with the following code:

CREATE TABLE IF NOT EXISTS account (rowID INT, user VARCHAR(20), pass VARCHAR(20))

But I don't know how to add here the part of inserting a new row. The row must be only inserted if the table is created at the moment, so I can't do the work in 2 queries.

Damith

try below code:

 using (SQLiteConnection con = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;"))
using (SQLiteCommand command = con.CreateCommand())
{
    con.Open();
    command.CommandText = "SELECT name FROM sqlite_master WHERE name='account'";
    var name = command.ExecuteScalar();

    // check account table exist or not 
    // if exist do nothing 
    if (name != null && name.ToString() == "account")
        return;
    // acount table not exist, create table and insert 
    command.CommandText = "CREATE TABLE account (rowID INT, user VARCHAR(20), pass VARCHAR(20))";
    command.ExecuteNonQuery();
    command.CommandText = "INSERT INTO account (rowID, user, pass) VALUES (0, '', '')";
    command.ExecuteNonQuery();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Getting Table doesn't exist error, but the table does exist (ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist)

来自分类Dev

#1146 - Table 'phpmyadmin.pma__tracking' doesn't exist

来自分类Dev

SQLite syntax to create index on a temp table?

来自分类Dev

SQLite CREATE TABLE [在“”附近:语法错误]

来自分类Dev

jQuery datatables add selected row to second table

来自分类Dev

Select ONE random row from Table1 where id does not exist in Table2

来自分类Dev

python sqlite3 DELETE FROM table row 如果存在

来自分类Dev

SQLITE TABLE创建错误

来自分类Dev

SQLITE NEW TABLE android

来自分类Dev

为什么ejabberd_config在create_table之后立即执行add_table_copy?

来自分类Dev

SQLite and Python: table already exists, no such table

来自分类Dev

Why doesn't django dumpdata include the django_migrations table?

来自分类Dev

CSS Table Cell doesn't become full width

来自分类Dev

Create a pivot table with PostgreSQL

来自分类Dev

如何避免在sqlite3中使用create table作为select命令更改字段的类型?

来自分类Dev

Android-SQLite-语法错误(代码1):,而在编译时:CREATE TABLE

来自分类Dev

sqlite3,python,CREATE TABLE-在“ /”附近:语法错误

来自分类Dev

如何将表名格式化为SQLite CREATE TABLE?

来自分类Dev

简单的CREATE TABLE命令中的奇怪语法错误-PostgreSQL,MySQL和SQLite

来自分类Dev

Android-SQLite-语法错误(代码1):,而在编译时:CREATE TABLE

来自分类Dev

sqlite3,python,CREATE TABLE-在“ /”附近:语法错误

来自分类Dev

table.row不是函数

来自分类Dev

Setting AllowBreakAcrossPages property for row in a table

来自分类Dev

Center the text in a row of an HTML table

来自分类Dev

Handle deselect row in table view

来自分类Dev

DataTables 函数- table.row.add ,不使用表格内的按钮(javascript)

来自分类Dev

将 CREATE TABLE、ADD COLUMNS、PRIMARY KEY、AS SELECT 和 WHERE 合并为一个操作

来自分类Dev

Create table stats from Json

来自分类Dev

Python/Pandas: create summary table

Related 相关文章

  1. 1

    Getting Table doesn't exist error, but the table does exist (ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist)

  2. 2

    #1146 - Table 'phpmyadmin.pma__tracking' doesn't exist

  3. 3

    SQLite syntax to create index on a temp table?

  4. 4

    SQLite CREATE TABLE [在“”附近:语法错误]

  5. 5

    jQuery datatables add selected row to second table

  6. 6

    Select ONE random row from Table1 where id does not exist in Table2

  7. 7

    python sqlite3 DELETE FROM table row 如果存在

  8. 8

    SQLITE TABLE创建错误

  9. 9

    SQLITE NEW TABLE android

  10. 10

    为什么ejabberd_config在create_table之后立即执行add_table_copy?

  11. 11

    SQLite and Python: table already exists, no such table

  12. 12

    Why doesn't django dumpdata include the django_migrations table?

  13. 13

    CSS Table Cell doesn't become full width

  14. 14

    Create a pivot table with PostgreSQL

  15. 15

    如何避免在sqlite3中使用create table作为select命令更改字段的类型?

  16. 16

    Android-SQLite-语法错误(代码1):,而在编译时:CREATE TABLE

  17. 17

    sqlite3,python,CREATE TABLE-在“ /”附近:语法错误

  18. 18

    如何将表名格式化为SQLite CREATE TABLE?

  19. 19

    简单的CREATE TABLE命令中的奇怪语法错误-PostgreSQL,MySQL和SQLite

  20. 20

    Android-SQLite-语法错误(代码1):,而在编译时:CREATE TABLE

  21. 21

    sqlite3,python,CREATE TABLE-在“ /”附近:语法错误

  22. 22

    table.row不是函数

  23. 23

    Setting AllowBreakAcrossPages property for row in a table

  24. 24

    Center the text in a row of an HTML table

  25. 25

    Handle deselect row in table view

  26. 26

    DataTables 函数- table.row.add ,不使用表格内的按钮(javascript)

  27. 27

    将 CREATE TABLE、ADD COLUMNS、PRIMARY KEY、AS SELECT 和 WHERE 合并为一个操作

  28. 28

    Create table stats from Json

  29. 29

    Python/Pandas: create summary table

热门标签

归档