【问题标题】:How to save values in database using Sqlite?如何使用 Sqlite 在数据库中保存值?
【发布时间】:2023-03-11 09:19:01
【问题描述】:

我正在尝试在 wp8 中做一个跟踪器应用程序。我想使用 Sqlite 将值保存在数据库中。所有值 r 工作(时间、距离和步速)。按下停止按钮后,我希望使用 Sqlite 将值发布到表格视图中,正如您在第二个屏幕中看到的那样。任何好的建议或链接表示赞赏。谢谢你。

【问题讨论】:

  • 这里是什么节奏,是分秒或分时分,请快点清除
  • 步速 = 分钟 / 距离
  • 正如我回答的那样,您可以以分钟为单位更改总时间,然后除以距离,然后将总分钟数存储在数据库中

标签: c# .net windows-phone-7 xaml windows-phone-8


【解决方案1】:

试试这个nokia developer site here。

给你一个小教程,教你如何在 windows 手机上使用 sqlite。

这段代码给你答案?

 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);
            }
        }

嗯,我看到这是一段检索代码。也许这个site helps you further。

下面的例子是一个插入:

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();
      }
   }
}

【讨论】:

  • 我试过你给的诺基亚网站链接,但它没有用。卡在了中间。无论如何,我使用 SQLiteWinRTPhone 完成了这项任务。
【解决方案2】:

我假设你的桌子是。

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

使用此语句插入值。

    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);

按照以下语句获取您的数据,我假设您知道如何在需要时将数据绑定到。我正在获取文本框中的值。

    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";
    }

之所以使用 Pace 作为双精度值,是因为我可以将此双精度值用作总分钟数,并且可以更改为 (以分钟和秒为单位)。对于任何其他问题,您可以随时询问。 要获得与您在问题中提出的完全相同的格式,您也应该像这样使用。

    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);

在获取信息时,请按此更改上述步骤。

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

【讨论】:

  • @RajVeer 感谢 raj 改进我的回答
猜你喜欢
  • 2011-08-10
  • 2014-01-27
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多