作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以在我的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/
我是一名优秀的程序员,十分优秀!