2016-07-06 47 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( '現在'));

相關問題