SQLiteDataReader错误,靠近“table”:语法错误

时间:2017-08-20 12:00:46

标签: c# sqlite

我的C#项目中有简单的SQLite数据库表 Database Screenshot

以下是我用来从DB检索数据的代码:

SQLiteConnection dbConnection;
dbConnection = new SQLiteConnection("Data Source=./new.db;");
dbConnection.Open();
if (dbConnection.State == System.Data.ConnectionState.Open)
     richTextBox3.Text = "Conn";
string sqlcommand = "SELECT age FROM table WHERE index=1";
SQLiteCommand command = new SQLiteCommand(sqlcommand, dbConnection);
SQLiteDataReader result = command.ExecuteReader();
if(result.HasRows)
{
      while (result.Read())
      {
           richTextBox1.Text = result.GetInt32(0) + " "+ result.GetString(1) + " " + result.GetInt32(2);
       }
 }

也许while循环不正确但我的问题是表格附近的语法错误。

2 个答案:

答案 0 :(得分:0)

尝试在表之间添加``因为表是保留字。您可以在reserved words

上查看所有保留字词
string sqlcommand = "SELECT `age` FROM `table` WHERE `index`='1'";

答案 1 :(得分:0)

正如@Rohit提到的table是SQLite中的关键字,但如果您仍想使用它,则可以更改查询,如下所示: 通过[table]

包围您的表名
string sqlcommand = "SELECT age FROM [table] WHERE index=1";

它也适用于SQLSERVER