how can I insert value into my sqlite3 database table? - iphone

I am writing a simple query as below
const char *sql = "insert into abc(name) values ('Royal')";
and this will insert each time 'Royal' into my 'name', so now I want to take input from user each time as names of hotel and wants to save them instead of 'Royal', so what should I do?
If you are not clear to my question, you may as me again,,,,,

this code is very simple for insert the value in sqlite3 table
-(void)writeValueInSettings:(NSMutableArray *)arrayvalue
{
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
database *objectDatabase=[[database alloc]init];
NSString *stringvalue2=[objectDatabase countValue];
[objectDatabase release];
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:");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Attention" message:#"You inserted successfully" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alert show];
[alert release];
}
[sql1 release];
sqlite3_close(myDatabase);
}
}

you need to connect to a database
- (sqlite3 *)database {
if (nil == db) {
NSString *path = <path to your database>;
int res = sqlite3_open([path UTF8String], &db);
if (res != SQLITE_OK){
// handle the error.
db = nil;
return nil;
}
}
return db;
}
then you can call this with the query
-(void)executeQuery:(NSString *)query{
sqlite3_stmt *statement;
if (sqlite3_prepare_v2([self database], [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {
sqlite3_step(statement);
}else{
// handle the error.
}
sqlite3_finalize(statement);
}
-(void)dealloc {
//close database connection
sqlite3_close(db);
db = nil;
[super dealloc];
}

-(NSArray *) executeSelect:(NSString *)query {
//Data search block****************************************
char *zErrMsg;
char **result;
int nrow, ncol;
sqlite3_get_table(
[self database], /* An open database */
[query UTF8String], /* SQL to be executed */
&result, /* Result written to a char *[] that this points to */
&nrow, /* Number of result rows written here */
&ncol, /* Number of result columns written here */
&zErrMsg /* Error msg written here */
);
NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:3];
for (int i=0; i<nrow; i++){
[returnArray addObject:[NSString stringWithUTF8String:result[ncol + i]]];
}
sqlite3_free_table(result);
return returnArray;
}

Hi
if Your getting user inputs in userinput textfield
NSString *qry=[NSString stringwithformat:#"insert into abc(name) values (\"%#\")",userinput.text];
const char *sql = [qry UTF8string];
sqlite3 *contactDB;
const char *dbpath = [databasePath UTF8String]; // Convert NSString to UTF-8
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) == SQLITE_OK)
{ // SQL statement execution succeeded
}
} else {
//Failed to open database
}

Related

how to save the data in sqlite3 in iphone

i am trying to create the database using sqlite manager.But the values are not stored in database.If i click the save button the Alert Message will be displayed on like this "Data Insertion Failed".i am trying to rectify these problem.In this case i visit so many tutorials.But i cant rectify my problem.Yesterday onwards i am totally blocked to this issue.please give me any idea or suggestion how to save the data.Thanks for all to visit the question.T.C.
DataBase.m
// creation of DATABASE
-(BOOL)createDB
{
NSString *docsDir;
NSArray *dirPaths;
// Get the document directory
dirPaths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
docsDir=dirPaths[0];
// Build the path to the database file
databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"Feedback.db"]];
BOOL isSuccess=YES;
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:databasePath]==0)
{
const char *dbpath=[databasePath UTF8String];
if(sqlite3_open(dbpath, &database)==SQLITE_OK)
{
char *errMsg;
const char *sql_stmt= "create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)";
if(sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK)
{
isSuccess=NO;
NSLog(#"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else
{
isSuccess=NO;
NSLog(#"Failed to open/Create database");
}
}
return isSuccess;
}
// save data in the Database
-(BOOL) saveData:(NSString *)Traineeid Trainername:(NSString *)Trainername Traineename:(NSString *)Traineename Rating:(NSString *)Rating;
{
const char *dbpath=[databasePath UTF8String];
if(sqlite3_open(dbpath, &database)==SQLITE_OK)
{
NSString *insertSQL=[NSString stringWithFormat:#"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%#\", \"%#\", \"%#\")",[Traineeid integerValue],Trainername,Traineename,Rating];
const char *insert_stmt=[insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement)==SQLITE_DONE)
{
return YES;
}
else
{
return NO;
}
sqlite3_reset(statement);
}
return NO;
}
FeebBackForm.m
-(IBAction)saveData:(id)sender
{
BOOL success=NO;
NSString *alertString = #"Data Insertion failed";
if (Traineeid.text.length>0 &&Trainername.text.length>0 &&Traineename.text.length>0 &&Rating.text.length>0)
{
success=[[DBManager getSharedInstance]saveData:Traineeid.text Trainername:Trainername.text Traineename:Traineename.text Rating:Rating.text];
}
else
{
alertString = #"Enter all fields";
}
if (success == NO)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertString message:nil
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Your putting string quotes around your int params in your sql statement.
This may not be your only issue but you should bind params. It looks like you're putting quotes around integer values in the sql insert statement which are defined as integers in your table.
"create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)"
"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%#\", \"%#\", \"%#\")"
Notice your double quotes around ints.
Also, log out the path to your database (even if running in simulator). Go to the sqlite cmd line and ensure the db exists and the empty table is there. This helps in troubleshooting.
Finally, take a look # the fmdb sqlite wrapper - it helps using sqlite but it's code also shows good patterns for using sqlite raw if that's your preference.
Here's a similar function from one of my samples which shows how to bind params. You should also finalize what you prepare:
- (void)updateContact: (Contact*)contact error:(NSError**)error
{
if (![self ensureDatabaseOpen:error])
{
return;
}
NSLog(#">> ContactManager::updateContact");
// prep statement
sqlite3_stmt *statement;
NSString *querySQL = #"update contacts set name=?,address=?,phone=? where id=?";
NSLog(#"query: %#", querySQL);
const char *query_stmt = [querySQL UTF8String];
// preparing a query compiles the query so it can be re-used.
sqlite3_prepare_v2(_contactDb, query_stmt, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [[contact name] UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(statement, 2, [[contact address] UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(statement, 3, [[contact phone] UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_int64(statement, 4, [[contact id] longLongValue]);
NSLog(#"bind name: %#", [contact name]);
NSLog(#"bind address: %#", [contact address]);
NSLog(#"bind phone: %#", [contact phone]);
NSLog(#"bind int64: %qi", [[contact id] longLongValue]);
// process result
if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(#"error: %#", sqlite3_errmsg(_contactDb));
}
sqlite3_finalize(statement);
}

how to save the tabledata in sqlite in iphone

i am trying to store the data in table using sqlite manager.But the values are not stored in database.If i click the save button the error message will be displayed on like this Failed to open/create database. I cant understand how to solve the problem.Anybody please give me an idea how to solve this problem.Thanks in advance.
DBManager.m
-(BOOL)createDB {
NSString *docsDir;
NSArray *dirPaths;
// Get the document directory
dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask,YES);
docsDir=dirPaths[0];
// Build the path to the database file
databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"Feedback.db"]];
BOOL isSuccess=YES;
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:databasePath]==0) {
const char *dbpath=[databasePath UTF8String];
if(sqlite3_open(dbpath, &database)==SQLITE_OK) {
char *errMsg;
const char *sql_stmt= "create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)";
if(sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK) {
isSuccess=NO;
NSLog(#"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess=NO;
NSLog(#"Failed to open/Create database");
}
}
return isSuccess;
}
// save data in the Database
-(BOOL) saveData:(NSString *)Traineeid Trainername:(NSString *)Trainername Traineename:(NSString *)Traineename Rating:(NSString *)Rating; {
const char *dbpath=[databasePath UTF8String];
if(sqlite3_open(dbpath, &database)==SQLITE_OK) {
NSString *insertSQL=[NSString stringWithFormat:#"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%#\", \"%#\", \"%#\")",[Traineeid integerValue],Trainername,Traineename,Rating];
const char *insert_stmt=[insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement)==SQLITE_DONE) {
return YES;
}
else {
return NO;
}
sqlite3_reset(statement);
}
return NO;
}
Ratingviewcontroller.m
-(IBAction)saveData:(id)sender {
BOOL success=NO;
NSString *alertString = #"Data Insertion failed";
if (Traineeid.text.length>0 &&Trainername.text.length>0 &&Traineename.text.length>0 &&Rating.text.length>0 ) {
success=[[DBManager getSharedInstance]saveData:Traineeid.text Trainername:Trainername.text Traineename:Traineename.text Rating:Rating.text];
}
else {
alertString = #"Enter all fields";
}
if (success == NO) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: alertString message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
You are not using the documents directory but the documentation directory. Use this NSDocumentsDirectory instead of NSDocumentationDirectory. Or even better, use the caches directory NSCachesDirectory because documents directory sync with iCloud and Apple might not like it since Documents directory should contain only user-downloaded files.

How can i insert encrypted data to sqlite. i'm getting an error while inserting encrypted data

How can i insert encrypted data to sqlite. i'm getting an error while inserting encrypted data. bcos the encrypted so many single quotes and double quotes so while am creating my sql the string is breaking. is there any other way to insert data without data lose.
also i'm afraid to use add slashes method bsoc it may alter my actual encrypted data. Can anyone give me a suggestion.. Also please find my insert query function below
-(BOOL) insertItemData:(NSString *)encryptedData folderId:(NSString *)folderId
{
bool giveBackValue = 0;
database = [[[DBConnection alloc] init] autorelease];
if(sqlite3_open([[database filePath] UTF8String], &db) == SQLITE_OK)
{
NSString *sql = [[[NSString alloc] initWithFormat:#"INSERT INTO tbl_content (FolderId, Content) VALUES ('%#', '%#');", folderId, encryptedData] autorelease];
NSLog(#"%#",sql);
char *sqlError;
if(sqlite3_exec(db, [sql UTF8String], nil, nil, &sqlError) == SQLITE_OK)
{
giveBackValue = 1;
}
else
{
//Query exec failed
}
}
else
{
//DB Open failed
}
return giveBackValue;
}
My select query function
-(void)getFirstJsonListInFolder:(NSString *)folderId listCarrier:(NSMutableArray **)listCarrier
{
database = [[[DBConnection alloc] init] autorelease];
NSMutableArray *dummyListCarrier = [[[NSMutableArray alloc] init] autorelease];
if (sqlite3_open([[database filePath] UTF8String], &db) ==SQLITE_OK)
{
NSString *sql = [[NSString alloc] initWithFormat:#"SELECT Content from tbl_content WHERE FolderId = '%#'", folderId];
sqlite3_stmt *result;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &result, nil) == SQLITE_OK)
{
while (sqlite3_step(result) == SQLITE_ROW)
{
char *contentList = (char *)sqlite3_column_text(result, 0);
NSString *contentListString = [[NSString alloc] initWithUTF8String:contentList];
[dummyListCarrier addObject:contentListString];
[contentListString release];
}
}
else
{
//Query exec failed
}
}
else
{
//DB Open failed
}
*listCarrier = dummyListCarrier;
}
You should avoid directly assigning values to the columns in sql statements. Instead you should use prepared statement and bind values to it.
NSString *sql = #"INSERT INTO tbl_content (FolderId, Content) VALUES ('?', '?');";
char *sql = (char *) [sql UTF8String];
sqlite3_bind_text(stmt, 1, [Content UTF8String], -1, SQLITE_TRANSIENT);
*This is not complete code

implement tableview to show the data of database

I am making a small iPhone application in which I have implemented the database concept which is used to store name, contact no, email id. I Want that whenever I user save any contact It get displayed on table view. In -(IBAction)retriveall:(id)sender action I am retriving all the data from database and storing it into array. Now I want to display all the data in tableview.
How can I do this? Please help me.
-(IBAction)retriveall:(id)sender
{
[self retrive2];
for (int i =0; i< [namearray count]; i++)
{
NSLog(#"Name is:%#",[namearray objectAtIndex:i]);
NSLog(#"Password is:%#",[passarray objectAtIndex:i]);
}
}
-(IBAction)retrive:(id)sender
{
[self retrive1];
two.text = databasecolorvalue;
UIAlertView *favAlert=[[UIAlertView alloc] initWithTitle:#"" message:#"Retrived" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[favAlert show];
[favAlert release];
}
-(void)retrive1
{
databaseName = #"med.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
#try {
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement="select pswd from Login where username = ? ";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
// Create a new animal object with the data from the database
sqlite3_bind_text(compiledStatement, 1, [one.text UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
databasecolorvalue =[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
#catch (NSException * ex) {
#throw ex;
}
#finally {
}
}
You should declare an mutable array as an instance variable something like,
NSMutableArray * colors;
Initialize it in the viewDidLoad method and later alter your retrive1 method to add to the colors array.
-(void)retrive1
{
/* Clearing existing values for newer ones */
[colors removeAllObjects]
/* Get database path */
#try {
[..]
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
[colors addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)]];
}
[..]
}
#catch (NSException * ex) { #throw ex; }
#finally { }
}
And you'll have to implement the rest of the table view methods in the usual manner using the array as the source.
You have to create object with all data and implementation of UITableViewDataSource protocol for table view and set it in TableView property dataSource. After you havecall UITableView method
-(void)reloadData;

Open two tables from one SQLite db in one iPhone Class?

small clarification is this possible to open two tables from one sqlite db in one iphone class??? but i can't open it please give me the solution i'm a beginner
here i tried coding
- (void)viewDidLoad {
[super viewDidLoad];
list = [[NSMutableArray alloc] init];
if ([material isEqualToString:#"Derlin"]) {
[self sel1];
}
else if([material isEqualToString:#"Helers"]){
[self sel2];
}
}
-(void)sel1 {
[self createEditableCopyOfDatabaseIfNeeded];
NSLog(#"numberOfRowsInSection");
sqlite3_stmt *statement = nil; // create a statement
const char *sql = "Select * from material"; //create a query to display in the tableView
if(sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK)
{
NSLog(#"sqlite3_open");
if(sqlite3_prepare_v2(database, sql,-1, &statement, NULL)!=SQLITE_OK)
NSAssert1(0,#"Error Preparing Statement",sqlite3_errcode(database));
else
{
while(sqlite3_step(statement) == SQLITE_ROW) // if the connection exists return the row of the query table
{
achemical = [NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,0)];
arates = [NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,1)];
anotes = [NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,2)];
Chemical * chemic = [[Chemical alloc] initWithName:achemical rates:arates notes:anotes];
[list addObject:chemic];
[chemic release];
}
}
}
sqlite3_finalize(statement);
}
-(void)sel2 {
[self createEditableCopyOfDatabaseIfNeeded];
NSLog(#"numberOfRowsInSection");
sqlite3_stmt *statement = nil; // create a statement
const char *sql = "Select * from material1"; //create a query to display in the tableView
if(sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK)
{
NSLog(#"sqlite3_open");
if(sqlite3_prepare_v2(database, sql,-1, &statement, NULL)!=SQLITE_OK)
NSAssert1(0,#"Error Preparing Statement",sqlite3_errcode(database));
else
{
while(sqlite3_step(statement) == SQLITE_ROW) // if the connection exists return the row of the query table
{
achemical = [NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,0)];
arates = [NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,1)];
anotes = [NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,2)];
Chemical * chemic = [[Chemical alloc] initWithName:achemical rates:arates notes:anotes];
[list addObject:chemic];
[chemic release];
}
}
}
sqlite3_finalize(statement);
}
thanks in advance
Sure, it's easy. Download FMDB, add it to your project, and add two FMResultSet objects in your class, one for each (query against each) table.