2016-07-06 89 views
0

我想在我的sqlite数据库中插入日期和时间,但它不工作。在sqlite中插入当前日期和时间

这是我的DB

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')); 
+1

你有什么错误?你忘了告诉我们。 –

+0

这是否工作'“(DATETIME('now'))”'? –

+0

@Navoneel它不工作。 – esquizoide

回答

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> 
+0

我想直接写入数据库使用sqlite3,EXE。你可以给我和例子在sqlite3中使用? – esquizoide

+0

@esquizoide我给了你一个我如何使用它的例子 –

0

一个朋友帮我解决这个查询句子。

应当如下:

INSERT INTOBitácora酒店(ID,农布雷,日志,出生日期)VALUES(1, '里卡多卡雷尼奥', '引物交',DATETIME( '现在'));

相关问题