SELECT count(*) FROM sqlite_master WHERE type='table' and name='" + TableName+ "'";
這裡的 name 後面接的就是 table name
查詢後會顯示數目, 0表示沒有, 1 表示有, 應該不會有一樣名稱的table 所以只有 0 或 1
兩種
sqlite_master 還有其他的用法
Directory.GetCurrentDirectory();
GetCurrentDirectory 會取得現在的資料夾位置,如果是用捷徑的話,就會回傳捷徑所在的資料夾
而
StartupPath 都是會回傳 程式確切的資料夾位置UPDATE table_name
SET column_name1 = value1,
column_name2 = value2, ...
[WHERE condition]
javascript:void(0)
public void UpdateExampleTable(SQLiteCommand cmd)
{
try
{
/// Set drop table command
cmd.CommandText = "Update [tbl] set id=1 where id=5";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("update OK");
}
catch
{
MessageBox.Show("update Error");
}
}
DELETE FROM employee WHERE id = 100;
TRUNCATE TABLE employee;
但 SQLite 沒有 truncate 但可以用
DELETE FROM employee 的語法達成同樣效果
DROP TABLE employee;
public void DeleteExampleTable(SQLiteCommand cmd)
{
try
{
/// Set Delete command
cmd.CommandText = "DELETE FROM [tbl] where id=3";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("delete OK");
}
catch
{
MessageBox.Show("delete Error");
}
}
public void DeleteExampleTable(SQLiteCommand cmd)
{
try
{
/// Set Delete command
cmd.CommandText = "DELETE FROM [tbl]";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("delete OK");
}
catch
{
MessageBox.Show("delete Error");
}
}
public void DROPExampleTable(SQLiteCommand cmd)
{
try
{
/// Set drop table command
cmd.CommandText = "DROP TABLE [table]";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("Drop OK");
}
catch
{
MessageBox.Show("Drop Error");
}
}
public void GetDataFromAccountTable(SQLiteCommand cmd)
{
// Set command
cmd.CommandText = "SELECT * FROM [tbl]";
/// CommandBehavior -> using System.Data;
/// Read all information
using (SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
/// Read information by each record
/// GetValue(by index), the index is begin 0 to the number of Field-1
while (dr.Read())
{
MessageBox.Show("第"+ dr.GetValue(0)+" 條:" + dr.GetValue(1));
}
}
}
public void InserDataInAccountTableTest(SQLiteCommand cmd)
{
try
{
for (int i = 0; i < 5; i++){
/// Set Commend
cmd.CommandText = string.Format("INSERT INTO [tbl] VALUES ({0}, 'Test{1}')", i,i);
/// Execute Commend
cmd.ExecuteNonQuery();
}
}
catch{ MessageBox.Show("insert Error");
}
}
public void LoginSqliteDatabase()
{
/// Set Database Root
string DatabaseRoot = "c:\\test.db";
/// Set Database Password
string DB_Password = "password";
/// Open Database
/// Version is sqlite version
SQLiteConnection cnn = new SQLiteConnection("Data Source="+ DatabaseRoot+";"+ "Version = 3; Password ="+DB_Password);
/// Open connect
cnn.Open();
/// Define SQLite Command object
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = cnn;
CreatExampleTable(cmd);
}
public void CreatExampleTable(SQLiteCommand cmd)
{
try
{
/// Set Creat table command
cmd.CommandText = "create table tbl(one, two)";
/// IF have exist show error message
cmd.ExecuteNonQuery();
}
catch
{
Console.WriteLine("error");
}
}
public void CreatSqliteDatabase()
{
/// Set Database Root
string DatabaseRoot = "c:\\test.db";
/// Set Database Password
string DB_Password = "password";
/// Check file is exits
/// File.Exists() -> using System.IO;
if (File.Exists(DatabaseRoot))
{
MessageBox.Show("Already exists.");
}
else
{
/// Creat table
SQLiteConnection.CreateFile(DatabaseRoot);
/// Open Database
SQLiteConnection cnn = new SQLiteConnection("Data Source="+DatabaseRoot);
/// Open connect
cnn.Open();
/// Set default Password
cnn.ChangePassword(DB_Password);
}
}