这是我的代码:
//-- check for d/b; create it if it doesn't exist
NSString *docsDir;
NSArray *dirPaths;
NSMutableArray *sqliteArray = [NSMutableArray array];
[sqliteArray addObject: @"CREATE TABLE IF NOT EXISTS SiteData (SITE_ID TEXT PRIMARY KEY NOT NULL, STA1 TEXT "
"TBM TEST, SITE_DESC TEXT, REMARKS TEXT, LOOP_COUNT INTEGER, DATE TEXT)" ];
[sqliteArray addObject: @"INSERT INTO SiteData (SITE_ID, LOOP_COUNT) VALUES('','')"];
[sqliteArray addObject: @"CREATE TABLE Readings (SITE_ID TEXT REFERENCES SiteData, LOOP_NBR TEXT, LOOP_CLOSED BINARY, "
"SEQ INTEGER, STA TEXT, BS TEXT, FS TEXT, DESC TEXT, SSBS TEXT, SSFS TEXT)" ];
[sqliteArray addObject: @"INSERT INTO Readings (SITE_ID, SEQ) VALUES ('','')"];
[sqliteArray addObject: @"CREATE TABLE EmailAddress (EMAIL_ADDRESS TEXT)"];
[sqliteArray addObject: @"INSERT INTO EmailAddress (EMAIL_ADDRESS) VALUES ('Enter email address here')" ];
// get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
docsDir = [dirPaths objectAtIndex: 0];
// build path to the d/b file
databasePath =[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"r64.sdb"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if([filemgr fileExistsAtPath:databasePath] == NO) {
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &dbLevelingData) == SQLITE_OK) {
char *errMsg;
int i;
for(i = 0; i < [sqliteArray count]; i++) {
if(sqlite3_exec(dbLevelingData, [sqliteArray objectAtIndex:i] , NULL, NULL, &errMsg) != SQLITE_OK)
status.text = @"Failed to create table";
}
sqlite3_close(dbLevelingData);
}
else
status.text = @"Failed to open/create database";
}
在“if(sqlite3_exe ...”语句中,我收到以下构建错误:
Implicit conversion of an Objective-C pointer to 'const char *' is disallowed with ARC
为什么(我没有看到任何'const'char)?以及如何解决? (注意:我从另一个例子中复制了基本格式...... NSMutableArray不一定是可变的,如果那样会有所帮助)
答案 0 :(得分:5)
根据此page,sqlite3_exec()
定义如下:
int sqlite3_exec(sqlite3*,const char *, int (*)(void*,int,char**,char**), void *, char **);
但是,您将其调用为:
int sqlite3_exec(sqlite3 *, id, int(*)(void *)(void *, int, char**, char **), void *, char **);
因此,如果您将函数调用更改为:
sqlite3_exec(dbLevelingData, [[sqliteArray objectAtIndex:i] UTF8String], NULL, NULL, &errMsg)
您的代码应该可以正常工作(基本上,我们会将您的NSString
转换为const char *
)。