在sqlite中插入当前日期和时间

时间:2016-07-06 15:31:51

标签: c# sqlite

我正在尝试在我的sqlite数据库中插入日期和时间,但它无法正常工作。

这是我的数据库

CREATE TABLE `bitacora` (
    `id`    INTEGER,
    `log`   TEXT,
    `nombre`    VARCHAR(20),
    `fecha` TEXT
);

我正在尝试以下查询;

INSERT INTO bitacora (id, nombre, log, fecha)
VALUES (1, 'ricardo carreño', 'primer post', (DATETIME('now'));

2 个答案:

答案 0 :(得分:0)

为什么不设置var并在值列表中调用它。

var DateTime = DateTime.Now;

以下是我如何插入SQLite的示例:

using System.Data.SQLite;
private void buttonAddNewItemtbl1AddButton_Click(object sender, RoutedEventArgs e)

{
    var tbl1CreatedDateTime = DateTime.Now;
    {
        var ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

        using (var DbConnection = new SQLiteConnection(ConnString))
        {
            try
            {
                const string insertIntotbl1TableString = "INSERT INTO [tbl1] (tbl1ID, tbl1EnvironmentType, tbl1URL, tbl1Server, tbl1CreatedDate, tbl1Deleted) VALUES (@tbl1ID, @tbl1EnvironmentType, @tbl1URL, @tbl1Server, @tbl1CreatedDate, @tbl1Deleted)";
                var insertIntotbl1Table = new SQLiteCommand(insertIntotbl1TableString);

                insertIntotbl1Table.Connection = DbConnection;

                insertIntotbl1Table.Parameters.AddWithValue("@tbl1ID", Guid.NewGuid().ToString());
                insertIntotbl1Table.Parameters.AddWithValue("@tbl1EnvironmentType", ComboBoxtbl1EnvironmentType.Text);
                insertIntotbl1Table.Parameters.AddWithValue("@tbl1URL", TextBoxtbl1Url.Text);
                insertIntotbl1Table.Parameters.AddWithValue("@tbl1Server", TextBoxtbl1ServerName.Text);
                insertIntotbl1Table.Parameters.AddWithValue("@tbl1CreatedDate", tbl1CreatedDateTime);
                insertIntotbl1Table.Parameters.AddWithValue("@tbl1Deleted", "0");

                try
                {
                    DbConnection.Open();
                    insertIntotbl1Table.ExecuteNonQuery();
                    DbConnection.Close();
                    MessageBox.Show("Successfully added");
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

在我的app.config中,它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ConnString" providerName="System.Data.SQLite" connectionString="Data Source=C:\Temp\SQLiteDB.db"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
</configuration>

答案 1 :(得分:0)

一位朋友帮我修复了查询句子。

应该如下:

INSERT INTO bitacora(id,nombre,log,fecha)VALUES(1,'ricardocarreño','primer post',DATETIME('now'));