【发布时间】:2011-09-26 03:48:21
【问题描述】:
在使用 Cold Fusion 15 年后,我开始从下往上学习 iPad 开发。我对 Ansi C 和 xCode 感到很舒服,但对使用 SQLite 进行下一步感到困惑。
我已经使用 razorSQL 构建了一个数据库 (Airports.sqlite),并将其安装在与 main.c 相同的目录中,我还安装了合并的 sqlite3.h 和 sqlite3.h 文件。
一切编译正常,但是当我运行时收到以下消息...
select 语句中的错误 select Length from Runways order by Length desc limit 5 [no such table: Runways]。
数据库中肯定有 Runways 表。有人可以让我直截了当吗?代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "weightbalance.h"
sqlite3* db;
int first_row;
int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names) {
int i;
int *p_rn = (int*)p_data;
if (first_row) {
first_row = 0;
for(i=0; i < num_fields; i++) {
printf("%20s", p_col_names[i]);
}
printf("\n");
for(i=0; i< num_fields*20; i++) {
printf("=");
}
printf("\n");
}
(*p_rn)++;
for(i=0; i < num_fields; i++) {
printf("%20s", p_fields[i]);
}
printf("\n");
return 0;
}
void select_stmt(const char* stmt) {
char *errmsg;
int ret;
int nrecs = 0;
first_row = 1;
ret = sqlite3_exec(db, stmt, select_callback, &nrecs, &errmsg);
if(ret!=SQLITE_OK) {
printf("Error in select statement %s [%s].\n", stmt, errmsg);
}
else {
printf("\n %d records returned.\n", nrecs);
}
}
void sql_stmt(const char* stmt) {
char *errmsg;
int ret;
ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
if(ret != SQLITE_OK) {
printf("Error in statement: %s [%s].\n", stmt, errmsg);
}
}
int main() {
sqlite3_open("Airports.sqlite", &db);
if(db == 0) {
printf("Could not open database.");
return 1;
}
printf("\nSelecting Airports with the longest runways.\n\n");
select_stmt("select Length from Runways order by Length desc limit 5");
sqlite3_close(db);
return 0;
}
【问题讨论】:
-
请在保存后查看您的问题,看看您是否可以阅读 - 在这种情况下需要将代码格式化为代码