【问题标题】:SQLite: Column 'date' does not belong to tableSQLite:列“日期”不属于表
【发布时间】:2013-12-26 05:13:33
【问题描述】:

我有一个包含以下列的 SQLite 数据库:_id、displayName、日期

使用以下 csharp 代码我正在尝试在 Sqlite 数据库中选择行,它适用于除日期之外的所有列!:

    try
    {
    SQLiteConnection mDBConnection = 
    new SQLiteConnection("Data Source=C:\\Users\\x86\\Desktop\\db forms\\instances.db");
        mDBConnection.Open();

        SQLiteCommand mycommand = new SQLiteCommand(mDBConnection);
        mycommand.CommandText = "SELECT _id, displayName, datetime(date) FROM instances";
        SQLiteDataReader reader = mycommand.ExecuteReader();
        dt.Load(reader);
        reader.Close();
        mDBConnection.Close();

        foreach(DataRow row in dt.Rows){
           string id = row["_id"].ToString();
           string displayName = row["displayName"].ToString();
           DateTime date = (DateTime)row["date"]; //Error Here: Column 'date' does not belong to table instances.
           string datefromDb = row["date"].ToString(); // Same Error with this line...
           MessageBox.Show(id + " " + displayName + " ");

        };            
        }



    catch (SQLiteException SqliteException)
    {
        MessageBox.Show(SqliteException.ToString());
    }

我收到此错误:列“日期”不属于表实例。 “日期”列实际上确实存在于数据库中。如果我不在 datetime(...) 中包含日期列,它会在 dt.Load(reader) 处给出“字符串未被识别为有效的 DateTime”;

更新:这是数据库...它的列和类型。

数据库中的日期数据。

【问题讨论】:

  • 也许你需要像datetime(date) as date这样的别名
  • Yes vendetta " datetime(date, 'localtime') as date " 有效,它现在返回日期,但完全符合预期,即 1387539933510(就像它们保存在数据库中一样)。有什么想法吗?
  • 你的数据库中日期列的列数据类型是什么?
  • 它的类型是日期,它的名字也是'日期'
  • 请查看更新后的问题

标签: c# database sqlite


【解决方案1】:

如下修改你的sql

  mycommand.CommandText = "SELECT _id, displayName, datetime(date) as date FROM instances";

否则,您的 select 语句将列名称返回为 datetime(date),通过使用别名 [as date] 确保列名称从选择查询返回,列名称为 date

更新:

  mycommand.CommandText = "SELECT _id, displayName, datetime(date/1000, 'unixepoch') as date FROM instances";

【讨论】:

  • Damith 它可以工作,它现在返回日期,但格式不正确,数据库日期是 13 位时间戳,例如1387539933510 现在它返回类似于:1698789-19650109-20 -19:-12:-55,有什么想法可以让我的数据库中的日期值完全一样吗?谢谢!
  • 现在它返回 2013-12-20 11:45:33,这是正确的 YYYY MM DD HH:MM:SS 日期时间,但仍然需要 unixepoch 或时间戳,即 1387539933510 就像它们一样存储在数据库中。
【解决方案2】:

试试这个:

mycommand.CommandText = "SELECT _id, displayName, datetime(date, localtime) date FROM instances";

这条线的状态

mycommand.CommandText = "SELECT _id, displayName, datetime(date) FROM instances";

localtime 会将您的 date 字符串转换为 DateTime。

【讨论】:

  • 它有效,但日期格式不正确:(即 1387539933510
  • @Ayub 我对 SQLLite 了解不多,但是当我用谷歌搜索时,我发现了一些与你更相关的东西,请查看此链接 sqlite.org/lang_datefunc.html。据此,我修改了建议的答案,再次检查。
【解决方案3】:

从这里替换你的 sql 语句。

mycommand.CommandText = "SELECT _id, displayName, datetime(date) as date FROM instances";

或者

替换

 DateTime date = (DateTime)row["date"];

来自

 DateTime date = (DateTime)row["datetime(date)"];

问题是您在 sql 查询中没有提到任何别名 datetime(date) as date "(as date)" 是别名。所以您的查询默认返回列名 datetime(date) 。提及别名后,您的列名将是 date

因此您可以在代码中使用上述两种逻辑。

【讨论】:

  • 这适用于 Shujaat,但我的数据库日期值如下:1387539933510 它返回类似:1698789-19650109-20 -19:-12:-55 对于相同的值,有没有办法我可以得到格式正确吗?
  • string datefromDb = row["date"].ToString("MM/dd/yyyy hh:mm tt");和 DateTime dt = Convert.ToDateTime(datefromDb);
  • Shujaat .toString 方法不带参数。我需要获取与数据库格式相同的值,即 1387539933510 不在 MM DD YYYY HH MM SS 中。
【解决方案4】:
    mycommand.CommandText = "SELECT _id, displayName, datetime(date) FROM instances";

不匹配

       DateTime date = (DateTime)row["datetime(date)"]; //Error Here: Column 'date' does not belong to table instances.
       string datefromDb = row["datetime(date)"].ToString(); // Same Error with this line...

确保使用列的确切名称。

13 位以上的注释是 Unix 时间戳,您正在请求格式化的日期时间。

【讨论】:

  • 我仔细检查了那个 drhunn,没有运气
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 2023-03-29
  • 2015-01-25
相关资源
最近更新 更多