sqlLite的数据库操作类

Posted codeDog123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlLite的数据库操作类相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SQLite;
namespace FaceAttendance
{
    class DBHelper
    {
        public SQLiteConnection conn;
        public DBHelper()
        {
            ConnectDatabase();
        }
        private void ConnectDatabase()
        {
            conn = new SQLiteConnection("Data Source=..\\..\\..\\faceinfo.db3; verson = 3;"); //创建数据库实例,指定文件位置  
            conn.Open(); //打开数据库,若文件不存在会自动创建
            if (conn == null) throw new Exception("数据库连接失败。请检查数据库文件是否存在。");
        }
        public void insertperson(int Id,string name,string sex,string position,string faceinfo,string des)
        {
            string sql = null;
            if (des ==null)
            {
                sql = "insert into info values(" + 14061143 + ",‘" + name + "‘,‘" + sex + "‘,‘" + position + "‘," + des + ");";
            }
            else
            {
                sql = "insert into info values(" + 14061143 + ",‘" + name + "‘,‘" + sex + "‘,‘" + position + "‘,‘" + des + "‘);";
            }
            SQLiteCommand command = new SQLiteCommand(sql, conn);
            command.ExecuteNonQuery();
            command.Dispose();
        }
        public void insertAttendance(int Id,string time)
        {
            string sql = "insert into signinfo values("+Id+",‘"+time+"‘);";
            SQLiteCommand command = new SQLiteCommand(sql, conn);
            command.ExecuteNonQuery();
            command.Dispose();
        }
        public List<peopleinfo> getpeopleinfo()
        {
            List<peopleinfo> faceRlt = new List<peopleinfo>();
            string sql = "SELECT * FROM info;";
            SQLiteCommand command = new SQLiteCommand(sql, conn);
            SQLiteDataReader reader = command.ExecuteReader();
            int j = 0;
            while (reader.Read())
            {
                j++;
                peopleinfo people = new peopleinfo();
                people.Id = reader.GetInt32(0);
                people.name = (string)reader["NAME"];
                people.sex = (string)reader["SEX"];
                people.position = (string)reader["JOB"];
                string fs = (string)reader["faceinfo"];
                string[] fsa = fs.Split(;);
                for (int i = 0; i < 128; i++)
                    people.faceinfo[i] = (float)Convert.ToDouble(fsa[i]);
                people.des = (string)reader["MSG"];
                faceRlt.Add(people);
            }
            command.Dispose();
            reader.Dispose();
            return faceRlt;
        }
        public void insertPersonRecord(string name,string time)
        {
            string sql = "insert into personRecord values(‘ " + name + "‘,‘" + time + "‘);";
            SQLiteCommand command = new SQLiteCommand(sql, conn);
            command.ExecuteNonQuery();
            command.Dispose();
        }
        public peopleinfo getPersonInfo(int Id)
        {
            string sql = "SELECT * FROM info WHERE Id = "+Id;
            SQLiteCommand command = new SQLiteCommand(sql, conn);
            SQLiteDataReader reader = command.ExecuteReader();
            reader.Read();
            peopleinfo people = new peopleinfo();
            people.Id = reader.GetInt32(0);
            people.name = (string)reader["NAME"];
            people.sex = (string)reader["SEX"];
            people.position = (string)reader["JOB"];
            people.des = (string)reader["MSG"];
            return people;
        }
    }
}

 

以上是关于sqlLite的数据库操作类的主要内容,如果未能解决你的问题,请参考以下文章

SqlLite数据库帮助类和基本DEMO

SqlLite数据库操作

Qt操作Sqlite类封装,及命令行导入csv文件到Sqlite数据库

Qt操作Sqlite类封装,及命令行导入csv文件到Sqlite数据库

sqllite小型数据库的使用

在Android中,如何将数据从类传递到相应的布局/片段文件?