以 mac 的作業系統為例:
將 excel 的 .xlsx 檔轉成 .csv 的步驟:
1. 建立一個 .xlsx 檔
2. 另存成 .csv 檔
3. 以 text 瀏覽器打開,在此我使用 TextWrangler 。到 Edit --> Document Options:
4. 將它 copy 入 Xcode 的 project 裡以用建立 sqlite 檔使用。
要確定 .csv 檔在 project 裡沒問題,可以點它,看中文有無亂碼:
如果見到如下,就沒問題:
學生;國文;英文
John;89;90
Mary;56;83
Peter;78;54
*PS 在 .xlsx 裡輸入資料時,請不要用英文的分號 (;)。如果真的需要分號,請以中文的方式輸入(;)。這樣才能確保在程式裡轉檔成sqlite檔案時不會出問題!!!
2012年10月7日
iOS -- sqlite3 的使用順序 & query 例子
使用 sqlite3 的一般步驟:
sqlite3_open() Open the database 打開資料庫
sqlite3_prepare() Create the SQL statement 準備建立好的資料庫指令 (query)
sqlite3_step() Execute the statement 執行指令
sqlite3_column() Fetch the result 取得結果
sqlite3_finalize() Destroy the statement 銷燬指令
sqlite3_close() Close the database 關閉資料庫
query 例子:
1. CREATE TABLE "main"."STARS" ("Rowid" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "Star" VARCHAR, "image" BLOB)
2. SELECT DISTINCT [column_name] FROM [table_name];
3. SELECT * FROM * WHERE * LIKE %$value%;
4. 找幾筆資料 (no. of rows in the table):
NSString *query = [NSString stringWithFormat:@"SELECT COUNT(%@) FROM %@ ", kSelectedColumn, kTablename];
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, [query UTF8String] , -1, &stmt, nil) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
int noOfRows = (int) sqlite3_column_int(stmt, 0);
NSLog(@"%d", noOfRows);
}
sqlite3_finalize(stmt2);
} else NSLog(@"never entered!!!");
sqlite3_close(database);
Fetch 的例子:
int sqlite3_column_count(sqlite3_stmt *pStmt);
可參考:http://www.sqlite.org/capi3ref.html
訂閱:
文章 (Atom)

