iOS SQLiteの使用概要
6331 ワード
1、SQLiteとは
SQLiteは軽量な組み込みデータベースです.リソースの消費量が非常に低く、組み込みデバイスでは数百Kのメモリだけで十分かもしれません.MySQL、PostgreSQLの2つの有名なデータベースよりも処理速度が速いです.
2、データベースとは
データベース(Database)は、データ構造に基づいてデータを整理、格納、管理する倉庫データベースで、(1)リレーショナル・データベース(主流)(2)オブジェクト・データベースの2種類に分類されます.
3.データの格納方法
データベースのストレージ構造はexcelに似ています.表(table)単位で、一般的には(1)新しい表(table)(2)複数のフィールド(column,列,属性)(3)複数行レコード(row,各行に複数のフィールドに対応する値を格納)を追加します.
4.SQLite文の特徴とキーワード:
特徴:(1)大文字と小文字を区別しない(例えば、データベースがuserとUsErを同じと考えている)(2)各文にセミコロン';の最後の部分
よく使われるキーワード:select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、indexなど注意:データベースでキーワードを使用してテーブル、フィールドを命名することはできません
SQLiteの使用についてコードで説明します
1)データベースを開く
2)表の作成
フォーマット:create tableテーブル名(フィールド名1フィールドタイプ1、フィールド名2フィールドタイプ2、...);create table if not existsテーブル名(フィールド名1フィールドタイプ1、フィールド名2フィールドタイプ2,...);
3)表の削除
フォーマット:drop tableテーブル名;drop table if existsテーブル名;
4)データの挿入
フォーマット:insert intoテーブル名(フィールド1、フィールド2、...)values(フィールド1の値、フィールド2の値、...);
5)データの削除
フォーマット:delete fromテーブル名;注意:削除されたのは、テーブル内のすべてのレコードが条件文を追加し、選択的に削除できます.
条件文の一般的なフォーマット:whereフィールド=値;if(フィールド==値)//2つ=whereフィールドis値は使用できません//is=whereフィールドに相当!=値;whereフィールドis not値;//is not相当!=whereフィールド>値;whereフィールド1=値andフィールド2>値;//andはC言語の&&whereフィールド1=ある値orフィールド2=ある値に相当する;//or C言語に相当する
6)データの更新
フォーマット:updateテーブル名setフィールド1=フィールド1の新しい値、フィールド2=フィールド2の新しい値、...;
7)クエリーデータ
フォーマット:SELECTフィールド1、フィールド2 FROM表示;
8)その他の機能
SQLiteは軽量な組み込みデータベースです.リソースの消費量が非常に低く、組み込みデバイスでは数百Kのメモリだけで十分かもしれません.MySQL、PostgreSQLの2つの有名なデータベースよりも処理速度が速いです.
2、データベースとは
データベース(Database)は、データ構造に基づいてデータを整理、格納、管理する倉庫データベースで、(1)リレーショナル・データベース(主流)(2)オブジェクト・データベースの2種類に分類されます.
3.データの格納方法
データベースのストレージ構造はexcelに似ています.表(table)単位で、一般的には(1)新しい表(table)(2)複数のフィールド(column,列,属性)(3)複数行レコード(row,各行に複数のフィールドに対応する値を格納)を追加します.
4.SQLite文の特徴とキーワード:
特徴:(1)大文字と小文字を区別しない(例えば、データベースがuserとUsErを同じと考えている)(2)各文にセミコロン';の最後の部分
よく使われるキーワード:select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、indexなど注意:データベースでキーワードを使用してテーブル、フィールドを命名することはできません
SQLiteの使用についてコードで説明します
1)データベースを開く
- (void)openSqlite{
//1. ( , )
// 1: (iOS Documents )
NSString *nsPath = [NSString stringWithFormat:@"%@/Documents/Person.db", NSHomeDirectory()];
const char *path = [nsPath UTF8String];
// 2:
// :
int ret = sqlite3_open(path, &_db);
//
if (ret == SQLITE_OK) {
NSLog(@" ");
}else{
NSLog(@" ");
}
}
2)表の作成
フォーマット:create tableテーブル名(フィールド名1フィールドタイプ1、フィールド名2フィールドタイプ2、...);create table if not existsテーブル名(フィールド名1フィールドタイプ1、フィールド名2フィールドタイプ2,...);
- (void)creatTable{
//1. sql
const char * sql = "CREATE TABLE IF NOT EXISTS t_Student(id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, score real DEFAULT 0, sex text DEFAULT ' ');";
//2. sql
// sqlite3_exec 、 、 ; sql
// 1: ( )
// 2: sql
// :
int ret = sqlite3_exec(_db, sql, NULL, NULL, NULL);
//3.
if (ret == SQLITE_OK) {
NSLog(@" ");
}else{
NSLog(@" ");
}
}
SQLite :
integer:
real:
text:
blob: ( )
: Sqlite , integer , ( )
, 、 ,
3)表の削除
フォーマット:drop tableテーブル名;drop table if existsテーブル名;
- (void)deleteTable {
const char *sql = "DROP TABLE if EXISTS t_Student;";
int ret = sqlite3_exec(_db, sql, NULL, NULL, NULL);
if (ret == SQLITE_OK) {
NSLog(@" ");
} else {
NSLog(@" ");
}
}
4)データの挿入
フォーマット:insert intoテーブル名(フィールド1、フィールド2、...)values(フィールド1の値、フィールド2の値、...);
- (void)insertData{
//1. sql
//=========== =========
const char * sql = "INSERT INTO t_Student (name,score,sex) VALUES (' ',65,' ');";
//========== =======
NSMutableString * mstr = [NSMutableString string];
for (int i = 0; i < 50; i++) {
NSString * name = [NSString stringWithFormat:@"name%d", i];
CGFloat score = arc4random() % 101 * 1.0;
NSString * sex = arc4random() % 2 == 0 ? @" " : @" ";
NSString * tsql = [NSString stringWithFormat:@"INSERT INTO t_Student (name,score,sex) VALUES ('%@',%f,'%@');", name, score, sex];
[mstr appendString:tsql];
}
// OC C
sql = mstr.UTF8String;
//2. sql
int ret = sqlite3_exec(_db, sql, NULL, NULL, NULL);
//3.
if (ret==SQLITE_OK) {
NSLog(@" ");
}else{
NSLog(@" ");
}
}
5)データの削除
フォーマット:delete fromテーブル名;注意:削除されたのは、テーブル内のすべてのレコードが条件文を追加し、選択的に削除できます.
条件文の一般的なフォーマット:whereフィールド=値;if(フィールド==値)//2つ=whereフィールドis値は使用できません//is=whereフィールドに相当!=値;whereフィールドis not値;//is not相当!=whereフィールド>値;whereフィールド1=値andフィールド2>値;//andはC言語の&&whereフィールド1=ある値orフィールド2=ある値に相当する;//or C言語に相当する
- (void)deleteData{
//1. sql
const char * sql = "DELETE FROM t_Student WHERE score < 10;";
//2. sql
int ret = sqlite3_exec(_db, sql, NULL, NULL, NULL);
//3.
if (ret == SQLITE_OK) {
NSLog(@" ");
}else{
NSLog(@" ");
}
}
6)データの更新
フォーマット:updateテーブル名setフィールド1=フィールド1の新しい値、フィールド2=フィールド2の新しい値、...;
7)クエリーデータ
フォーマット:SELECTフィールド1、フィールド2 FROM表示;
- (void)selectData{
//1. sql
const char * sql = "SELECT name,score FROM t_Student;";
//2. sql
// 1:
// 2:sql
// 3:sql (-1 )
// 4: ( )
sqlite3_stmt * stmt;
// 5:NULL
// :
int ret = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
if (ret == SQLITE_OK) {
NSLog(@" ");
//
//sqlite3_step
while (sqlite3_step(stmt) == SQLITE_ROW) {
// 1:
// 2:
const unsigned char * name = sqlite3_column_text(stmt, 0);
double score = sqlite3_column_double(stmt, 1);
NSLog(@"%s %.2lf", name, score);
}
}else{
NSLog(@" ");
}
}
8)その他の機能
:
select 1 , 2 , … from ;
select 1 , 2 as , … from as ;
select . 1, . 2, … from ;
select count ( ) from ;
select count ( * ) from ;
order by
select * from t_student order by ;
select * from t_student order by age ;
( ), ( )
select * from t_student order by age desc ; //
select * from t_student order by age asc ; // ( )
select * from t_student order by age asc, height desc ;
( ), ( )
limit , 10
: select * from limit 1, 2 ;
:
select * from t_student limit 4, 8 ;
: 4 , 8
,
not null : null
unique :
default :
( : , )
:
, ,
, ,
(Primary Key, PK)
t_student id ,