12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLiteの基本的な使い方

Posted at

条件付きselectの結果を複数行扱う場合はこのような感じになります。
sqlite3_bind_intのindexが1から始まるのが気持ち悪い・・・。

	sqlite3 *database = nil;
	int result = sqlite3_open([path fileSystemRepresentation], &database);
	if (result != SQLITE_OK) {
		return NO;
	}

	int id_min = 100;
	const char *sql = "select id, name from tb1 where id > ?";
	sqlite3_stmt *statement = nil;
	int result = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
	if (result != SQLITE_OK) {
		sqlite3_close(database);
		return NO;
	}
	result = sqlite3_bind_int(statement, 1, id_min);
	if (result != SQLITE_OK) {
		sqlite3_finalize(statement);
		sqlite3_close(database);
		return NO;
	}

	while (sqlite3_step(statement) == SQLITE_ROW) {
		int id = sqlite3_column_int(statement, 0);
		NSString *name = nil;
		const unsigned char *str = sqlite3_column_text(statement, 1);
		if (str && strlen((const char *)str)) {
			name = [NSString stringWithUTF8String:(const char *)str];
		}
		// id, nameをどこかに格納する.
	}

	result = sqlite3_finalize(statement);
	if (result != SQLITE_OK) {
		sqlite3_close(database);
		return NO;
	}

	result = sqlite3_close(database);
	if (result != SQLITE_OK) {
		return NO;
	}

	return YES;
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?