hai sto cercando di creare un database SQLite a livello di codice in fase di esecuzione. qualcuno può dire come crearlo in iphone sdk.crea sqlite db a livello di codice in iphone sdk
risposta
Basta chiamare la funzione sqlite3_open per creare un database se non esiste alcun database sul percorso.
// generate databasePath programmatically
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// your code here
}
pubblicare un commento se avete bisogno di più codice di esempio su questo
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"if");
}
sqlite3_close(contactDB);
} else
{
NSLog(@"else");
}
}
[filemgr release];
}
-(IBAction)table
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
// if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE LIST (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"tables failed");
// status.text = @"Failed to create table";
}
sqlite3_close(contactDB);
}
else
{
NSLog(@"tables failed");
//status.text = @"Failed to open/create database";
}
}
[filemgr release];
}
importare nel file .m #import
importazione a .m sqlite3.h file di #import e aggiungere quadro nel progetto ur libsqlite3.0.dylib
innanzitutto creare la classe NSObject e denominarla Database. in .h classe
@interface database : NSObject
{
NSString *databasePath;
NSString *databaseName;
sqlite3 *myDatabase;
NSArray *documentPaths;
NSString *documentsDir;
}
// --- metodi iniziali -------
-(void)createDatabaseIfNeeded;
// --------------- --path find method --------------------- //
-(void)pathFind;
// ------------- ---- valore di scrittura ---------------------- //
-(void)writeValueInSettings:(NSMutableArray *)arrayvalue;
// ------------------- recuperare valore dalla tabella impostazione ------------ //
-(NSMutableArray *)fetchValue;
// ------------------- valore di aggiornamento --------------------- //
-(void)updateSetting:(NSArray *)arr;
classe
.m scrittura
-(id)init
{
if((self=[super init]))
{
[self createDatabaseIfNeeded];
}
return self;
}
//-----------create database if needed method--------------//
-(void)createDatabaseIfNeeded
{
[self pathFind];
BOOL success;
NSFileManager *filemgr = [NSFileManager defaultManager];
success=[filemgr fileExistsAtPath:databasePath];
if (success)return;
NSLog(@"not success");
//Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[filemgr copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
// ---------------- percorso trovare ----------------- //
-(void)pathFind
{
databaseName = @"accDataBase.DB";
// Get the path to the documents directory and append the databaseName
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
}
// ------------------ scrivere il valore in un contesto ---------------- //
-(void)writeValueInSettings:(NSMutableArray *)arrayvalue
{
NSLog(@"%@",arrayvalue);
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
database *objectDatabase=[[database alloc]init];
NSString *stringvalue2=[objectDatabase countValue];
[objectDatabase release];
int intvalue1=[stringvalue2 intValue];
intvalue1=intvalue1+1;
NSLog(@"opened");
NSString *sql1;
sql1=[[NSString alloc] initWithFormat:@"insert into setting values('%i','%i','%i','%@','%i','%i','%@','%i','%i','%i','%i','%i','%i','%@');",intvalue1,
[[arrayvalue objectAtIndex:0] intValue],[[arrayvalue objectAtIndex:1] intValue],[arrayvalue objectAtIndex:2],[[arrayvalue objectAtIndex:3] intValue],[[arrayvalue objectAtIndex:4]intValue ],[arrayvalue objectAtIndex:5],[[arrayvalue objectAtIndex:6]intValue],[[arrayvalue objectAtIndex:7]intValue ],[[arrayvalue objectAtIndex:8] intValue],[[arrayvalue objectAtIndex:9] intValue],[[arrayvalue objectAtIndex:10]intValue ],[[arrayvalue objectAtIndex:11]intValue],[arrayvalue objectAtIndex:12]];
char *err1;
if (sqlite3_exec(myDatabase,[sql1 UTF8String],NULL,NULL,&err1)==SQLITE_OK)
{
NSLog(@"value inserted:");
}
[sql1 release];
sqlite3_close(myDatabase);
}
} // ------------ recuperare tutto il valore ------------- //
-(NSMutableArray *)fetchValue
{
NSMutableArray *list=nil;
list=[[[NSMutableArray alloc]init] autorelease];
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
NSString *sql=[NSString stringWithFormat: @"select * from setting where primaryKey=1"];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(myDatabase, [sql UTF8String], -1,&statement, NULL)==SQLITE_OK)
{
if(sqlite3_step(statement)==SQLITE_ROW)
{
for(int i=0;i<=13;i++)
{
char *pass=(char*)sqlite3_column_text(statement,i);
NSString *msg=[[NSString alloc]initWithUTF8String:pass];
[list addObject:msg];
[msg release];
}
}
sqlite3_finalize(statement);
}
sqlite3_close(myDatabase);
}
return list;
}
// ----- ----------- metodo di impostazione tabella di aggiornamento --------------- //
-(void)updateSetting:(NSArray *)arr
{
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
NSLog(@"opened");
sqlite3_stmt *compiledStmt;
// NSLog(@"%@",arr);
NSString *sqlStmt=[NSString stringWithFormat:@"UPDATE setting SET ragular=%i,cycle=%i, flow='%@', hour=%i,minute=%i,formate='%@' ,tenminute=%i ,thirtyminute=%i,sixtymin=%i, twentymin=%i, fourtyfivemin=%i ,other='%@',formatemessage ='%@' WHERE primaryKey=%i;",[[arr objectAtIndex:0]intValue],[[arr objectAtIndex:1]intValue],[arr objectAtIndex:2],[[arr objectAtIndex:3]intValue],[[arr objectAtIndex:4]intValue],[arr objectAtIndex:5],[[arr objectAtIndex:6]intValue],[[arr objectAtIndex:7]intValue],[[arr objectAtIndex:8]intValue],[[arr objectAtIndex:9]intValue],[[arr objectAtIndex:10]intValue],[arr objectAtIndex:11],[arr objectAtIndex:12],1];
// NSLog(@"%@",sqlStmt);
if(sqlite3_prepare_v2(myDatabase, [sqlStmt UTF8String],-1,&compiledStmt, NULL)==SQLITE_OK)
{
NSLog(@"updateding......cycle");
}
sqlite3_step(compiledStmt);
sqlite3_close(myDatabase);
}
}
wow. grazie per l'aiuto davvero bello .... – ravinder521986
- 1. Come creare database SQLite a livello di codice?
- 2. Crea Meeting Exchange a livello di codice
- 3. Crea directory a livello di codice
- 4. Crea pulsante circolare in Iphone SDK
- 5. Distribuzione del DB sqlite sull'aggiornamento dell'app iPhone
- 6. Informazioni protette contenute su iPhone SQLite DB
- 7. iPhone SDK - Aggiungi un UINavigationController a livello di programmazione
- 8. Crea dl sqlite al livello di programmazione se non esiste?
- 9. OpenXML 2 SDK - Documento Word - Crea elenco puntato a livello di codice
- 10. UILabel - impostazione font - typeface a livello di codice in iPhone
- 11. Crea sottodomini con apache e molla a livello di codice
- 12. C# - Crea tabella SQL Server a livello di codice
- 13. Crea entità a livello di codice (dati principali)
- 14. crea un controllo a livello di codice utilizzando MFC
- 15. Crea file su Onedrive a livello di codice da C#?
- 16. Crea database SQLite in Android
- 17. iPhone SDK NSString a NSDate
- 18. Caratteri speciali a SQLite Query (in iPhone)
- 19. Visualizzazione griglia in iPhone SDK
- 20. java.lang.OutOfMemoryError sull'archiviazione di immagini in sqlite db
- 21. Crea routeValues (tipo anonimo) a livello di codice in ASP.NET MVC 3
- 22. Kit eventi iPhone: crea un EKCalendar al livello di programmazione?
- 23. iPhone SDK: controllo siti Web in UIWebView a livello di programmazione
- 24. Come ottenere il nome del dispositivo a livello di programmazione in sdk per iPhone?
- 25. iPhone SDK - Come far scorrere UITableView a livello di programmazione con l'animazione?
- 26. È possibile convertire a livello di codice database SQLite in istruzioni SQL in C/C++?
- 27. C# crea report a livello di programmazione
- 28. iPhone - "Apri in" in SDK?
- 29. Come differenziare a livello di codice tra iphone 4 e iphone 4S?
- 30. GameKit in iPhone SDK 3.0
qualsiasi tutorial su questo argomento, saurabh. Per favore, puoi darci grazie –