esquizoide
esquizoide

Reputation: 15

Inserting current date and time in sqlite

I'm trying to insert date and time in my sqlite database but it is not working.

This is my DB

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

And I'm trying the following query;

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

Upvotes: 0

Views: 2044

Answers (2)

Scott E
Scott E

Reputation: 175

Why don't you set a var and call it within your values list.

var DateTime = DateTime.Now;

Here is an example of how I am inserting into 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);
            }
        }
    }
}

In my app.config it looks like this:

<?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>

Upvotes: 0

esquizoide
esquizoide

Reputation: 15

A friend helped me fix the query sentence.

It should be as following:

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

Upvotes: 0

Related Questions