简体   繁体   English

如何使用Sqlite在数据库中保存值?

[英]How to save values in database using Sqlite?

I am trying to do a tracker application in wp8. 我正在尝试在wp8中做一个跟踪器应用程序。 I want to save the values in database using Sqlite. 我想使用Sqlite将值保存在数据库中。 All the values r working (time, distance and pace). 所有值都起作用(时间,距离和步速)。 After pressing Stop button, I want the values to be posted in a table view using Sqlite as u can see in the second screen. 按下停止按钮后,我希望使用Sqlite将值发布到表视图中,如您在第二个屏幕中看到的那样。 Any good suggestions or links are appreciated. 任何好的建议或链接表示赞赏。 Thank u. 感谢你。

在此处输入图片说明

Try this nokia developer site here . 在此处尝试此诺基亚开发人员网站

Gives you a small tutorial how to use sqlite on windows phones. 给您一个小教程,介绍如何在Windows Phone上使用sqlite。

This piece of code gives you the answer? 这段代码为您提供了答案?

 private void Insert_Click_1(object sender, RoutedEventArgs e)
        {
            // Create a new task.
            Task task = new Task()
            {
                Title = TitleField.Text,
                Text = TextField.Text,
                CreationDate = DateTime.Now
            };
            /// Insert the new task in the Task table.
            dbConn.Insert(task);
            /// Retrieve the task list from the database.
            List<Task> retrievedTasks = dbConn.Table<Task>().ToList<Task>();
            /// Clear the list box that will show all the tasks.
            TaskListBox.Items.Clear();
            foreach (var t in retrievedTasks)
            {
                TaskListBox.Items.Add(t);
            }
        }

hm, i see this is a retrieval piece of code. 嗯,我看到这是一段检索代码。 Maybee this site helps you further . Maybee这个网站可以帮助您进一步发展

The following example is an insert: 以下示例是一个插入:

public void Initialize()
{
   using ( var db = new SQLite.SQLiteConnection( _dbPath ) )
   {
      db.CreateTable<Customer>();

      //Note: This is a simplistic initialization scenario
      if ( db.ExecuteScalar<int>( 
             "select count(1) from Customer" ) == 0 )
      {

         db.RunInTransaction( () =>
         {
            db.Insert( new Customer() { 
               FirstName = "Jon", LastName = "Galloway" } );
            db.Insert( new Customer() { 
               FirstName = "Jesse", LastName = "Liberty" } );
         } );
      }
      else
      {
         Load();
      }
   }
}

I'm assuming your table is. 我假设你的桌子在。

    public class HistoryTable
    {
        public string date { get; set; }
        public string time { get; set; }
        public double pace { get; set; }
        public double distance { get; set; }
    }

Insert values using this statement. 使用此语句插入值。

    string date = DateTime.Now.ToShortDateString();
    string time = DateTime.Now.ToShortTimeString();
    double pace = 16;
    double distance = 4;
    SQLiteConnection conn = new SQLiteConnection(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Database.sqlite"));
    conn.Execute("Insert into HistoryTable values(?,?,?,?)", date, time, pace, distance);

Fetch your data as below statement, I'm assuming that you know how to bind the data in if there is need. 如下所示获取数据,假设您知道如何在需要时绑定的数据。 I'm taking the values in textbox. 我正在文本框中输入值。

    SQLiteConnection conn = new SQLiteConnection(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Database.sqlite"));
    var result = conn.Table<HistoryTable>();
    foreach (var val in result)
    {
        TimeSpan pace = TimeSpan.FromMinutes(val.pace);
        TextBoxDate.Text = val.date;
        TextBoxTime.Text = val.time;
        TextBoxPace.Text = pace.Minutes + ":" + pace.Seconds;
        TextBoxDistance.Text = val.distance + " Km";
    }

The reason why I used Pace as double because I can use this double value as total minutes and can be changed as (in minutes and seconds). 之所以将Pace用作double,是因为我可以将此double值用作总分钟数,并且可以将其更改为 (以分钟和秒为单位)。 For any other queries you can ask any time. 对于任何其他查询,您可以随时询问。 To get exactly the same format as you ask in your question you should use like this also. 要获得与您在问题中提出的格式完全相同的格式,您也应该使用类似的格式。

    string date = string.Format("{0:00}.{1:00}.{2:00}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
    string time = DateTime.Now.ToString("HH:mm:ss");
    double pace = 16.5;
    double distance = 4;
    SQLiteConnection conn = new SQLiteConnection(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Database.sqlite"));
    conn.Execute("Insert into HistoryTable values(?,?,?,?)", date, time, pace, distance);

At the time of fetching info, please change the above steps by this. 在获取信息时,请据此更改上述步骤。

    TextBoxDistance.Text = string.Format("{0:0.00}Km", val.distance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM