gpt4 book ai didi

iphone - 谁能告诉我为什么我的代码无法创建数据库和表,然后在其中存储数据。

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

因此,我的代码假设是创建一个数据库和表,然后将我的位置数据存储在表中。它会击中我创建的所有正确日志,例如“创建表”和“插入位置”,但是当我去查找文件时,它要么(a)不存在,要么(b)那里但没有位置数据。

我开始使用CoreData,但是它很快变成了一场噩梦,因此我改为直接使用sqlite。

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

locationManager =[[CLLocationManager alloc] init];

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 10;
[locationManager startUpdatingLocation];



// Create a string containing the full path to the bold2.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"bold2.sql"];

// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databasehandle) == SQLITE_OK)
{
// Create the database if it doesn't yet exists in the file system
if (!databaseAlreadyExists)
{
// Create the LOCATION table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS LOCATION (ID INTEGER PRIMARY KEY AUTOINCREMENT, latitude DOUBLE, longitude DOUBLE, timeStamp DATE)";
char *error;
if (sqlite3_exec(databasehandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Created Table");
}
}


这是位置代表,我在这里获取新位置并将其插入数据库。奇怪的是,它实际上命中了“插入位置”日志。

    -(void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{

NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO location (latitude, longitude, timeStamp) VALUES (%g, %g, %g)",newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];
char *error;
if ( sqlite3_exec(databasehandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Location inserted. %g", newLocation.coordinate.latitude);
}

else NSLog(@"Error: %s", error);
}

最佳答案

我解决了因为我要远程处理到minimac,所以数据库的文件路径被隐藏了。我只是将正确的文件路径粘贴到了文件夹中,然后找到了正确的文件。

关于iphone - 谁能告诉我为什么我的代码无法创建数据库和表,然后在其中存储数据。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220819/

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