sqlite的创建数据库,表,插入查看数据
IOS sqlite数据库操作。步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1、新建项目sqliteDemo,添加使用sqlite的库libsqlite3.dylib 2、sqlite 的方法 sqlite3 *db, 数据库句柄,跟文件句柄FILE
IOS sqlite数据库操作。步骤是:
先加入sqlite开发库libsqlite3.dylib,
新建或打开数据库,
创建数据表,
插入数据,
查询数据并打印
1、新建项目sqliteDemo,添加使用sqlite的库libsqlite3.dylib
2、sqlite 的方法
sqlite3 *db, 数据库句柄,跟文件句柄FILE很类似
sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句
sqlite3_open(), 打开数据库,没有数据库时创建。
sqlite3_exec(), 执行非查询的sql语句
Sqlite3_step(), 在调用sqlite3_prepare后,使用这个函数在记录集中移动。
Sqlite3_close(), 关闭数据库文件
还有一系列的函数,用于从记录集字段中获取数据,如
sqlite3_column_text(), 取text类型的数据。
sqlite3_column_blob(),取blob类型的数据
sqlite3_column_int(), 取int类型的数据
3、获取沙盒目录,并创建或打开数据库。
viewController.h头文件添加一个成员变量,并包含头文件sqlite3.h
[cpp] view plain copy
-
#import
-
#import
- @interface ViewController : UIViewController
- {
- sqlite3 *db;
- }
- @end
在.m文件 定义宏,方面后面使用
[cpp] view plain copy
- #define DBNAME @”personinfo.sqlite”
- #define NAME @”name”
- #define AGE @”age”
- #define ADDRESS @”address”
- #define TABLENAME @”PERSONINFO”
[cpp] view plain copy
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documents = [paths objectAtIndex:0];
- NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
- if (sqlite3_open([database_path UTF8String], &db) != SQLITE_OK) {
- sqlite3_close(db);
- NSLog(@”数据库打开失败”);
- }
sqlite3_open,如果数据不存在,则创建。运行。这是在沙盒目录下能看到数据库文件(如何打开模拟器沙盒目录请参考:IOS学习之IOS沙盒(sandbox)机制和文件操作(一))
4、创建数据表
创建一个独立的执行sql语句的方法,传入sql语句,就执行sql语句
[cpp] view plain copy
- -(void)execSql:(NSString *)sql
- {
- char *err;
- if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
- sqlite3_close(db);
- NSLog(@”数据库操作数据失败!”);
- }
- }
创建数据表PERSONINFO的语句
[cpp] view plain copy
- NSString *sqlCreateTable = @”CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, address TEXT)”;
- [self execSql:sqlCreateTable];
运行程序,数据表创建了。怎么知道数据表创建了呢?我们用火狐的Sqlite Manager插件工具打开数据库文件看看。可以在火狐浏览器里安装这个插件。打开
四个字段都出现是表中了。
5、插入数据:
[cpp] view plain copy
- NSString *sql1 = [NSString stringWithFormat:
- @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
- TABLENAME, NAME, AGE, ADDRESS, @"张三", @"23", @"西城区"];
- NSString *sql2 = [NSString stringWithFormat:
- @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
- TABLENAME, NAME, AGE, ADDRESS, @"老六", @"20", @"东城区"];
- [self execSql:sql1];
- [self execSql:sql2];
运行程序,插入两条数据,用火狐的sqlite工具查看
6、查询数据库并打印数据
[cpp] view plain copy
- NSString *sqlQuery = @”SELECT * FROM PERSONINFO”;
- sqlite3_stmt * statement;
- if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
- while (sqlite3_step(statement) == SQLITE_ROW) {
- char *name = (char*)sqlite3_column_text(statement, 1);
- NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
- int age = sqlite3_column_int(statement, 2);
- char *address = (char*)sqlite3_column_text(statement, 3);
- NSString *nsAddressStr = [[NSString alloc]initWithUTF8String:address];
- NSLog(@”name:%@ age:%d address:%@”,nsNameStr,age, nsAddressStr);
- }
- }
- sqlite3_close(db);
打印结果:
[cpp] view plain copy
- 2012-06-29 13:25:32.205 sqlitDemo[3587:f803] name:张三 age:23 address:西城区
- 2012-06-29 13:25:32.206 sqlitDemo[3587:f803] name:老六 age:20 address:东城区
最后关闭数据库。例子代码:http://download.csdn.net/detail/totogo2010/4400911
http://iosshare.cn/?p=1115

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The annual WWDC has ended, and iOS18 is undoubtedly the focus of everyone's attention. Currently, many iPhone users are rushing to upgrade to iOS18, but various system bugs are making people uncomfortable. Some bloggers said that you should be cautious when upgrading to iOS18 because "there are so many bugs." The blogger said that if your iPhone is your main machine, it is recommended not to upgrade to iOS18 because the first version has many bugs. He also summarized several system bugs currently encountered: 1. Switching icon style is stuck, causing the icon not to be displayed. 2. Flashlight width animation is often lost. 3. Douyin App cannot upload videos. 4. WeChat message push is delayed by about 10 seconds. 5 . Occasionally, the phone cannot be made and the screen is black. 6. Severe fever.

According to news on July 31, Apple issued a press release yesterday (July 30), announcing the launch of a new open source Swift package (swift-homomorphic-encryption) for enabling homomorphic encryption in the Swift programming language. Note: Homomorphic Encryption (HE) refers to an encryption algorithm that satisfies the homomorphic operation properties of ciphertext. That is, after the data is homomorphically encrypted, specific calculations are performed on the ciphertext, and the obtained ciphertext calculation results are processed at the same time. The plaintext after state decryption is equivalent to directly performing the same calculation on the plaintext data, achieving the "invisibility" of the data. Homomorphic encryption technology can calculate encrypted data without leaking the underlying unencrypted data to the operation process.

Thanks to netizens Ji Yinkesi, xxx_x, fried tomatoes, Terrence, and spicy chicken drumsticks for submitting clues! According to news on July 27, Apple today re-released the iOS/iPadOS 18 Beta 4 update for developers. The internal version number was upgraded from 22A5316j to 22A5316k. It is currently unclear the difference between the two Beta 4 version updates. Registered developers can open the "Settings" app, enter the "Software Update" section, click the "Beta Update" option, and then toggle the iOS18/iPadOS18 Developer Beta settings to select the beta version. Downloading and installing the beta version requires an Apple ID associated with a developer account. Reported on July 24, iO

Update: Saunders Tech has uploaded a tutorial to his YouTube channel (video embedded below) explaining how to install Fortnite and the Epic Games Store on an iPad outside the EU. However, not only does the process require specific beta versions of iO

Thanks to netizens Spicy Chicken Leg Burger, Soft Media New Friends 2092483, Handwritten Past, DingHao, Xiaoxing_14, Wowotou Eat Big Kou, Feiying Q, Soft Media New Friends 2168428, Slades, Aaron212, Happy Little Hedgehog, Little Earl, Clues for the little milk cat that eats fish! [Click here to go directly to the upgrade tutorial] According to news on July 24, Apple today pushed the iOS/iPadOS18 developer preview version Beta4 update (internal version number: 22A5316j) to iPhone and iPad users. This update is 15 days after the last release. . Carplay Wallpaper Apple has added wallpapers to CarPlay, covering light and dark modes. Its wallpaper style is similar to iPhone

Thanks to netizen Ji Yinkesi for submitting the clue! According to news on July 30, Apple today released the first developer beta version of iOS/iPadOS 18.1 and the second public beta version of iOS/iPadOS 18. It also released iOS 16.7.9 and iOS 15.8.3 updates for older iPhones. Apple wrote in the update logs for both versions: "This update provides important security fixes and is recommended for all users to install," but did not mention what was fixed. iOS16.7.9 Note: iOS16.7.9 is suitable for Apple iPhoneX, iPhone8 and iPhone8Plus. According to the document details disclosed by Apple, the above three models are expected to support

According to industry insider Mark Gurman, Apple’s Apple Intelligence will be postponed to October. In other words, it will be pushed first on iOS18.1. Apple iPhone 16 is expected to be released in September, so Apple Intelligence will not be pre-installed. 1. Apple Intelligence Apple Intelligence is a personal intelligence system that uses a powerful generative model to provide new functions for iPhone, iPad and Mac to assist users in communicating, working and expressing. 2. Natural language understanding The large model embedded in Apple Intelligence has a deep understanding of the meaning of language.

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs
