gpt4 book ai didi

iphone - 如何在iPhone中以编程方式插入和查看数据到sqlite数据库

转载 作者:行者123 更新时间:2023-12-03 18:39:41 24 4
gpt4 key购买 nike

我可以在我的sqlite数据库中创建表,但是无法在终端中查看该表我的代码

-(void)initDatabase
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"Database.sql"];

bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{

if (!databaseAlreadyExists)
{
// Create the PERSON table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS PERSON (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, BIRTHDAY DATE)";
char *error;
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
// Create the ADDRESS table with foreign key to the PERSON table
sqlStatement = "CREATE TABLE IF NOT EXISTS ADDRESS (ID INTEGER PRIMARY KEY AUTOINCREMENT, STREETNAME TEXT, STREETNUMBER INT, PERSONID INT, FOREIGN KEY(PERSONID) REFERENCES PERSON(ID))";
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Database and tables created.");
}
else
{
NSLog(@"Error: %s", error);
}
}
else
{
NSLog(@"Error: %s", error);
}
}
}

这是我的控制台输出 Database and tables created.
然后我尝试像这样在终端中打开该数据库
p-mac001:~ Th$ sqlite3 /Users/Th/Desktop/sample/Database.sql 
SQLite version 3.7.7 2011-06-25 16:35:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables

我看不到任何表格,请问我做错了什么。

最佳答案

我已经使用iPhone 5.0模拟器对此进行了测试,并且您的代码有效。

SQLite version 3.7.7 2011-06-25 16:35:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
ADDRESS PERSON

编辑:

这里有我的代码,就像您的代码一样:
#import <sqlite3.h>

@implementation FFViewController
-(void)initDatabase
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"Database.sql"];

bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
sqlite3 *databaseHandle = NULL;

if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{

if (!databaseAlreadyExists)
{
// Create the PERSON table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS PERSON (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, BIRTHDAY DATE)";
char *error;
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
// Create the ADDRESS table with foreign key to the PERSON table
sqlStatement = "CREATE TABLE IF NOT EXISTS ADDRESS (ID INTEGER PRIMARY KEY AUTOINCREMENT, STREETNAME TEXT, STREETNUMBER INT, PERSONID INT, FOREIGN KEY(PERSONID) REFERENCES PERSON(ID))";
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Database and tables created.");
}
else
{
NSLog(@"Error: %s", error);
}
}
else
{
NSLog(@"Error: %s", error);
}
}
}
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self initDatabase];
}

@end

关于iphone - 如何在iPhone中以编程方式插入和查看数据到sqlite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com