sqllite的使用

本文介绍了SQLite数据库的基础使用,包括安装、连接、创建表、插入数据、查询操作等关键步骤,帮助开发者快速掌握SQLite在应用程序中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sqllite的使用

  <appSettings>
  <!--查询时间-->
    <add key="SqliteTime" value="10" />
  </appSettings>
 public string SqliteTime = System.Configuration.ConfigurationManager.AppSettings["SqliteTime"].ToString();       
//查询十分钟内的时间
            FileInfo fi = new FileInfo(@"D:\After20190701\file.txt");
            if (fi.Exists)
            {
                FileInfo copyfi = new FileInfo(@"D:\After20190701\copyfile.txt");
                if (copyfi.Exists)
                {
                    File.Delete(@"D:\After20190701\copyfile.txt");
                    fi.CopyTo(@"D:\After20190701\copyfile.txt");
                }
                else
                {
                    fi.CopyTo(@"D:\After20190701\copyfile.txt");
                }
            }
            using (SQLiteConnection con = new SQLiteConnection(DB_PATH))
            {

                con.Open();
                string sqlStr = @"SELECT * FROM VisitorRecord t where datetime(t.VisitTime) > datetime('now','-"+ SqliteTime + " minutes','localtime') order by VisitTime desc limit 0,1";
                using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con))
                {
                    using (SQLiteDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                           string value = dr["Name"].ToString();
                           cbbName.Items.Add(value); //将市添加到comboBoxCity中
                        }
                    }
                }
            }
//c查询今天内的时间
SELECT  * FROM VisitorRecord where Status=1  and  strftime('%m-%d','now','localtime') = strftime('%m-%d',VisitTime)  order by VisitTime desc limit 0,1
//选择满足条件的第一条记录
select * from tablename limit 0,1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace LogisticsSystem
{
    public partial class Testfrm : Form
    {
        public Testfrm()
        {
            InitializeComponent();
        }
        //static readonly string DB_PATH = "Data Source=E:/database/sqlite/arena.db";
        //public static readonly string DB_PATH = "Data Source=D:/After20190701/Project/SQLiteSpy_1.9.14/VSCDB.db";
        public static readonly string DB_PATH = "Data Source=D:\\After20190701\\Project\\SQLiteSpy_1.9.14\\VSCDB";
        public string  Select()
        {
            string value = "";
            using (SQLiteConnection con = new SQLiteConnection(DB_PATH))
            {
                
                con.Open();
                string sqlStr = @"SELECT *
                                    FROM VisitorRecord";
                using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con))
                {
                    using (SQLiteDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                             value+= dr["Id"].ToString() + dr["Name"];
                        }
                    }
                }
            }
            return value;
        }
        private void Testfrm_Load(object sender, EventArgs e)
        {
            textBox1.Text=Select();
            //Delete();
            //Insert();
            //Select();
            //Update();
        }
        public static void Insert()
        {
            using (SQLiteConnection con = new SQLiteConnection(DB_PATH))
            {
                con.Open();
                string sqlStr = @"INSERT INTO VisitorRecord
                                  VALUES
                                  (
                                      7,
                                      '萨满'
                                  )";
                using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public static void Update()
        {
            using (SQLiteConnection con = new SQLiteConnection(DB_PATH))
            {
                con.Open();
                string sqlStr = @"UPDATE VisitorRecord
                                     SET Name = '盗贼'
                                   WHERE Id = 1";
                using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public static void Delete()
        {
            using (SQLiteConnection con = new SQLiteConnection(DB_PATH))
            {
                con.Open();
                string sqlStr = @"DELETE FROM hero";
                using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }

        //static void Main(string[] args)
        //{
            
        //}

     
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值