我从sqlitebrowser检查我成功创建了sqlite并将数据插入sqlite,但现在我的问题是我无法从sqlite读取数据。我是sqlite的新手。
我应该先打开sqlite,然后阅读吗?我的代码在哪里出错?
此代码用于打开现有的sqlite。
-(void)openSqlite{
NSString *docsDir = [NSString stringWithFormat:@"Questiondata.db"];
NSArray *dirPaths;
_databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Questiondata.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:_databasePath] == NO) {
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
char *errorMessage;
const char *sql_statement ="CREATE TABLE IF NOT EXISTS users (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDB,sql_statement,NULL,NULL,&errorMessage) != SQLITE_OK) {
NSLog(@"Failed to create the table");
}
sqlite3_close(_contactDB);
}
else{
NSLog(@"Fail to open/create the table");
}
}
}
这是从现有sqlite读取数据的代码。
-(void)readData{
NSLog(@"we came here");
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB)) {
NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM Questions WHERE QuestionNumber = 1"];
const char*query_statement = [querySQL UTF8String];
if (sqlite3_prepare_v2(_contactDB, query_statement, -1, &statement, NULL)== SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
NSString *addressfield = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)];
NSLog(@"finddata:%@",addressfield);
}else{
NSLog(@"notfinddatabase");
}
sqlite3_finalize(statement);
}else{
NSLog(@"failed to serache database");
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
NSLog(@"not be here");
}
}
答案 0 :(得分:0)
在openSqlite方法中
const char *sql_statement ="CREATE TABLE IF NOT EXISTS users (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
并在您的查询中
NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM Questions WHERE QuestionNumber = 1"];
表名和列名不相同。
地方变更" Questions
"用" users
" 。
并改变" QuestionNumber
"使用table column
。
可能,它会帮助你或感到自由。
答案 1 :(得分:0)
PIPEDRIVE_API_URL = "https://api.pipedrive.com/v1/"
route = 'persons/123'
api_token = '1234abcd1234abcd'
response, data = http.request(PIPEDRIVE_API_URL + route + '?api_token=' + api_token,
method='GET',
headers={'Content-Type': 'application/x-www-form-urlencoded'})
不会报告所有错误。例如,几乎任何文件名和路径都会没有错误地传递。
首先你必须检查一下,数据库是否在sqlite
?
它默认不存在。
也许它在主捆绑中?
试试这个:
Documents folder