2013年8月21日 星期三

使用sqlite


//插入user資料表,一筆email和名字的紀錄。
_dbCmd.CommandText = "INSERT INTO user(email,name) VALUES ('email','name')";
_dbCmd.ExecuteNonQuery();

dbCmd.CommandText = "INSERT INTO user(email,name) VALUES ('dd@com','許秝維')";
_dbCmd.ExecuteNonQuery();

//更新user資料表,將名為bbn的名字都改為Codelicious
_dbCmd.CommandText = "UPDATE user SET name = 'Codelicious' WHERE name = 'bbn'";
_dbCmd.ExecuteNonQuery();  

//刪除user資料表
cmd.CommandText = "Drop Table user";
cmd.ExecuteNonQuery();

//在user資料表中,搜尋名為'許秝維',且email為dd@com的紀錄
_dbCmd.CommandText = "SELECT ID, name,email FROM user where name='許秝維' and email='dd@com'";
SQLiteDataReader reader = _dbCmd.ExecuteReader();
if (reader.HasRows)
{
 while (reader.Read())
 {
     Console.WriteLine("ID: " + reader.GetInt32(0));
     Console.WriteLine("name: " + reader.GetString(1));
     Console.WriteLine("email: " + reader.GetString(2));
 }
}
reader.Close();

//取得最後一筆紀錄
_dbCmd.CommandText = "SELECT  * FROM " + TABLE_FILE + " ORDER BY id DESC";
SQLiteDataReader reader = _dbCmd.ExecuteReader();
if (reader.HasRows)
{
 while (reader.Read())
 {
     Console.WriteLine("ID: " + reader.GetInt32(0));
     Console.WriteLine("name: " + reader.GetString(1));
     Console.WriteLine("email: " + reader.GetString(2));
     break;
 }
}
reader.Close();

//建立資料表
//Check table file exist?
_dbCmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name='" + TABLE_FILE + "'";
dataReader = _dbCmd.ExecuteReader();
if (!dataReader.HasRows)
{
 dataReader.Close();
 _dbCmd.CommandText = "Create Table " + TABLE_FILE + " (id INTEGER PRIMARY KEY ASC, name ,path,tags,comment)";
 _dbCmd.ExecuteNonQuery();
}
else
{
 dataReader.Close();
}

//判斷屬性的資料型態
if (reader.HasRows)
{
    while (reader.Read())
    {
 for(int i=0;i<reader.FieldCount;i++)
 {
     if (reader.GetName(i).CompareTo("id") == 0)
     {
  if (reader.GetFieldType(i) == typeof(string))
  {
      dataResult[i]= reader.GetString(i);
  }
  else if (reader.GetFieldType(i) == typeof(Int64))
  {
      dataResult[i] = reader.GetInt64(i).ToString();
  }
  else if (reader.GetFieldType(i) == typeof(Int32))
  {
      dataResult[i] = reader.GetInt32(i).ToString();
  }
  else if (reader.GetFieldType(i) == typeof(Int16))
  {
      dataResult[i] = reader.GetInt16(i).ToString();
  }
  else if (reader.GetFieldType(i) == typeof(Boolean))
  {
      dataResult[i] = reader.GetBoolean(i).ToString();
  }
  else if (reader.GetFieldType(i) == typeof(Int64))
  {
      dataResult[i] = reader.getobj.GetInt64(i).ToString();
  }
  
     }
     
 }
 
 //Console.WriteLine("ID: " + reader.get.GetInt32(0));
 //Console.WriteLine("name: " + reader.GetString(1));
 //Console.WriteLine("email: " + reader.GetString(2));
    }
}
reader.Close();




沒有留言: