using System.Data.SQLite;
using System.Data;
using System.IO;
using System;
using System.Text;
namespace Sqlite_Demo
{
public class Sqlite_Help
{
public Sqlite_Help()
{
}
public enum data_type { varchar, }
private string table_name_out { get; set; }
public static SQLiteCommand command { get; set; }
public static SQLiteConnection sq { get; set; }
public string counll { get; set; }
public void init(string db_address, string db_name, string table_name)
{
table_name_out = table_name;
string db_path = db_address + @"\" + db_name;
if (!File.Exists(Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory) + @"\c_sharp_sqlite" + @"\c_sharp.db"))
{
// Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\Sqlite_Test" + @"\zz.db";
SQLiteConnection.CreateFile(db_path);//创建数据库
sq = new SQLiteConnection("data source=" + db_path);
sq.Open();
command = new SQLiteCommand();
command.Connection = sq;
command.CommandText = "CREATE TABLE " + table_name + "(id INTEGER PRIMARY KEY,name varchar,age varchar, sex varchar)";
command.ExecuteNonQuery();
sq.Close();
}
else
{
sq = new SQLiteConnection("data source=" + db_path);
command = new SQLiteCommand();
command.Connection = sq;
}
}//创建数据库和数据表
public bool insert_data(string name, string values)
{
sq.Open();
command.CommandText = "Insert into " + table_name_out + " (" + name + ")" + " values" + "(" + values + ")";
if (command.ExecuteNonQuery()>0)
{
sq.Close();
return true;
}
else
{
sq.Close();
return false;
}
}//插入数据
public bool delete_data()
{
sq.Open();
command.CommandText = "delete from " + table_name_out;
if (command.ExecuteNonQuery()>0)
{
sq.Close();
return true;
}
else
{
sq.Close();
return false;
}
}//清空数据表信息
public DataTable read_data()
{
sq.Open();
SQLiteDataAdapter adapter = new SQLiteDataAdapter("select * from "+table_name_out, sq);
DataTable dt = new DataTable();
adapter.Fill(dt);
sq.Close();
return dt;
}//读取数据表
public bool update(string name,string age,string sex,string id)
{
sq.Open();
command.CommandText = "update " + table_name_out + " set " + "name=" +"'"+ name+"'" + ",age=" + "'" + age + "'" + ",sex=" + "'" + sex + "'" + " where " + "id=" + id;
if (command.ExecuteNonQuery()>0)
{
sq.Close();
return true;
}
else
{
sq.Close();
return false;
}
}//修改数据
public void diy_sql(string sql)
{
sq.Open();
command.CommandText = sql;
try
{
command.ExecuteNonQuery();
sq.Close();
}
catch (Exception)
{
sq.Close();
}
}
}
}