Move unencrypted tables to encrypted sqlite database - ios

I want to know how to transfer my unencrypted sqlite tables to encrypted sqlite database. I'm using sqlcipher to encrypt a database.
I've tried this code, but it didn't work.
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent: #"contacts_enc.sqlite"];
sqlite3 *db;
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
const char* key = [#"1234" UTF8String];
sqlite3_key(db, key, strlen(key));
if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master", NULL, NULL, NULL) == SQLITE_OK) {
NSLog(#"password is correct, or, database has been initialized") ;
//START
sqlite3_exec(db, "ATTACH DATABASE 'contacts.sqlite' AS encrypted KEY '1234';", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE encrypted.account(_id,username,password);", NULL, NULL, NULL);
sqlite3_exec(db, "INSERT INTO encrypted.account SELECT * FROM account;", NULL, NULL, NULL);
sqlite3_exec(db, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
//END
}
}

SQLCipher provides a convenience function called sqlcipher_export(…) that will duplicate the contents of your database to another attached database. For your scenario, please review example 1 found here within the documentation.

Related

sqlcipher stopped working in iOS 10

I have use Sqlite (sqlcipher) in my iOS project for database. In iOS 9 all things are working perfectly. Now i have update new Xcode. But DB encryption is not working now.
sqlite3 *db1;
if (sqlite3_open([[self.databaseURL path] UTF8String], &db1) == SQLITE_OK) {
const char* key = [g_sqlite_key UTF8String];
AZLog(#"%s",key);
sqlite3_key(db1, key, (int)strlen(key));
if (sqlite3_exec(db1, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
AZLog(#"Password is correct, or a new database has been initialized");
} else {
AZLog(#"Incorrect password!");
}
sqlite3_close(db1);
}
Can anyone help me ?
Thanks in advance
You have to provide readwrite and open-create permission while encryption of database using sqlcipher.
sqlite3 *db1;
if (sqlite3_open_v2([[self.databaseURL path] UTF8String], &db1, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) == SQLITE_OK) {
const char* key = [g_sqlite_key UTF8String];
AZLog(#"%s",key);
sqlite3_key(db1, key, (int)strlen(key));
if (sqlite3_exec(db1, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
AZLog(#"Password is correct, or a new database has been initialized");
} else {
AZLog(#"Incorrect password!");
}
sqlite3_close(db1);
}
There were many changes with Xcode 8 and the new SDKs. Please refer to this guidance:
https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688

how to create a json object from the last insert to sqLite AND a database browser for sqlite of iOS

i got 2 main question.
First one; is there any database browser for sqlite that i'm using in my iOS application?
The second question - big one - is a bit complicated.
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: #"insider.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"create table if not exists userInfo (device_token text primary key, device_type text, carrier text, app_version text,os_version text, first_name text, last_name text,last_viewed_item text, last_added_item text, last_item_price_tag text, name_entered integer, login_screen integer, item_detailed_screen integer )";
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;
}
this is my createDB method that i call it my applicationDIdBecome active,
and i have several methods to get the items that will be saved to my database one of them ;
-(BOOL)getUserFirstName:(NSString *)firstName {
NSLog(#"User's first name is this %#",firstName);
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"insert into userInfo (first_name) values (\"%#\")", firstName];
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;
}
i have a lot of these kind of methods ( for each columns in my create table statement)
What i want is this;
I have a backend that use mysql as database.
I need to make the last insertion row and make it JSON format so I can manipulate it and write it to my backend server.
How can i do this?
Thanks in advance.
Cheres.
Regarding SQLite tool, the database format is binary compatible across platforms. So you can just run the app in the simulator, browse to the folder with the simulator files (which varies depending upon Xcode version: it's ~/Library/Application Support/iPhone Simulator in Xcode versions prior to 6, it's ~/Library/Developer/CoreSimulator/Devices in Xcode 6) and open up the database. If your Library folder is hidden, you can unhide it from the Terminal command line, chflags nohidden ~/Library.
You can use the Mac OS X sqlite3 command line app. Many people use the free Firefox SQLite Manager add-on. I personally use Base, a relatively inexpensive SQLite tool. Use whatever you want.
Regarding retrieving results from SQLite and creating JSON from that, the easiest approach is to build a NSDictionary with the values, which you can then pass to NSJSONSerialization method dataWithJSONObject, which can build the JSON body of the request.
So, presumably you'll have some model structure that captures the content in your thirteen columns of this local table. So, insert that data in the local table, and then write a different routine that takes that model data and creates your request to POST this to the server.
You need to determine what your web service API will look like as well as how you want to POST it (e.g. NSURLConnection, NSURLSession (for iOS 7 and later), AFNetworking, etc.), but this seems well beyond the scope of this question.
A couple of unrelated observations:
If you use sqlite3_exec, supplying a pointer to a char * for the error message, remember that you're responsible for releasing that. Also, you might as well log that error message so you know why it failed:
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) {
isSuccess = NO;
NSLog(#"Failed to create table: %s", errMsg);
sqlite3_free(errMsg);
}
See the sqlite3_exec documentation for more information.
I would advise against using stringWithFormat to build your INSERT statements. Always use ? placeholders in your SQL, and then manually bind values.
Also, getUserFirstName is returning immediately if the statement was successful. So, it's not freeing the memory associated with the sqlite3_prepare_v2 (which you should do via sqlite3_finalize, not sqlite3_reset). It's also not closing the database.
-(BOOL)insertUserFirstName:(NSString *)firstName
{
BOOL success = YES;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
const char *insert_stmt = "insert into userInfo (first_name) values (?)";
if (sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL) != SQLITE_OK) {
NSLog(#"prepare failure: %s", sqlite3_errmsg(database));
sqlite3_close(database);
return NO;
}
if (sqlite3_bind_text(statement, 1, [firstName UTF8String], -1, NULL) != SQLITE_OK) {
success = NO;
NSLog(#"bind 1 failure: %s", sqlite3_errmsg(database));
} else if (sqlite3_step(statement) != SQLITE_DONE) {
success = NO;
NSLog(#"step failure: %s", sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
statement = NULL;
sqlite3_close(database);
database = NULL;
}
return success;
}
Personally, I'd even advise against opening and closing the database with every SQLite call. But if you do, make sure you balance your open statements and close statements like above. You might want to also test to make sure that firstName is not nil (and if it is, call sqlite3_bind_null instead of sqlite3_bind_text).
It doesn't quite make sense to me that you're inserting only the user name. You'd presumably want to insert all of the column values at once.
When writing SQLite code, you might want to consider using FMDB, a thin Objective-C wrapper around SQLite C API, which greatly simplifies your life. When you tackle step 3, performing the sqlite3_bind_XXX calls for each of your thirteen columns, I think you'll really start to appreciate the power of something like FMDB.

Delete row from SQLLite database

I am trying to delete a row from my database "Code mentioned below", however it never deletes the row and the prepare statement also never seems to be called.
I can insert and update ok but for the life of me can't figure this out..
barcode is a string variable passed to the method from a editable tableview.
I can see the correct values when I log the deletSQL string but record is actually never removed.
For further info the barcode in the database is a primary key.
Also database paths are all correct and its calling the right db file.
Its just the delete function that is not working...
-(void)delete:(NSString *)barcode{
const char *dbpath = [databasePath3 UTF8String];
if (sqlite3_open(dbpath, &database3) == SQLITE_OK)
{
NSLog(#"Opened OK");
NSString *deleteSQL = [NSString stringWithFormat: #"delete from assets where assetBarcode=\"%#\'",barcode];
NSLog(#"%#",deleteSQL);
const char *sql = [deleteSQL UTF8String];
if(sqlite3_prepare_v2(database3, sql, 1, &deleteStatement, NULL) == SQLITE_OK)
{
NSLog(#"SQL OK?");
if (sqlite3_step(deleteStatement) == SQLITE_ROW)
{
sqlite3_bind_text(deleteStatement, 1, [barcode UTF8String], -1, SQLITE_TRANSIENT);
}
}
else{
NSLog(#"WHAAAATTTTT");
}
}
sqlite3_step(deleteStatement);
sqlite3_reset(deleteStatement);
sqlite3_close(database3);
}
the barcode columne in the database appears to be of type varchar
try this
-(void)delete:(NSString *)barcode {
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
if(deleteStatement == nil)
{
const char *sql = "DELETE FROM assets WHERE assetBarcode = ?";
if(sqlite3_prepare_v2(database, sql, -1, & deleteStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(deleteStatement, 1, [barcode UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(deleteStatement))
NSAssert1(0, #"Error while deleting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
//noteID = sqlite3_last_insert_rowid(database);
//Reset the delete statement.
sqlite3_reset(deleteStatement);
sqlite3_close(database);
deleteStatement = nil;
}
else
sqlite3_close(database);
}
Regarding the SQL string...
delete from assets where assetBarcode=\"%#\'
The parameter is being opened with a quote and closed with a single quote. Use either double or single quotes for both. Perhaps this is related to your issue?

Application crashes with Assertion Failure message while updating SQLite database

In my application i keep some data in local Sqlite db. Sqlite open, insert all working fine. but when i try to update data of a particular row/record if fails with ASSERTION FAILURE message..
* Assertion failure in -[gss_databaseHandler updateRecord::::], /Users/gss/Desktop/SalesPro copy/SalesPro/../gss_databaseHandler.m:210
NSString *databasePath;
// Method to open a database, the database will be created if it doesn't exist
-(void)initDatabase
{
// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
databasePath = [documentsDirectory stringByAppendingPathComponent:#"contact1"];
// 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 contactList table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS contactList (sapCustId TEXT, sapContactId TEXT, record_ID NUMERIC PRIMARY KEY, timestamp TEXT)";
char *error;
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(#"Database and tables created.");
}
else
{
NSLog(#"Error: in creating/opening database");
}
}
}
}
- (void) updateRecord:(int)recordID:(NSString *)sapCustId:(NSString *)sapContactId:(NSString *)timestamp {
[self initDatabase];
sqlite3_stmt *updateStmt = nil;
if(updateStmt == nil) {
const char *sql_stmt = "update contactList Set sapCustId = ?, sapContactId = ?, timestamp = ? Where record_ID = ?";
if(sqlite3_prepare_v2(databaseHandle, sql_stmt, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(databaseHandle));
}
sqlite3_bind_text(updateStmt, 0, [sapCustId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 1, [sapContactId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(updateStmt, 2, recordID);
sqlite3_bind_text(updateStmt, 3, [timestamp UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(databaseHandle));
sqlite3_finalize(updateStmt);
sqlite3_close(databaseHandle);
//Reclaim all memory here.
[sapContactId release];
[sapCustId release];
}
Pls help!..
So, a couple of thoughts:
As Prateek and Lefteris suggested, you should change the order of the bind calls so that they match the order of the fields in the SQL. They also start with an index of one, not zero. I've always found it odd that the index for sqlite3_bind calls start at one, whereas the index for sqlite3_column calls start at zero, but it's just the way it works.
While it's probably fine, if you ever do an assertion, you should generally make sure you first (a) finalize any successfully prepared statements; and (b) close the database. You don't want to risk leaving your database in an inconsistent state.
You probably should not release sapContactId or sapCustId here. You didn't retain or do anything to increase their retain count, so it's probably not prudent to be releasing them here. If you run your code through the static analyzer (press shift+command+B or choose "Analyze" from the product menu), you'll undoubtedly see suggestions/warnings there.
As outlined in the official SQLite An Introduction To the SQLite C/C++ Interface, your sequence of open, prepare_v2, step, finalize, and close is perfectly correct. You do not need to call sqlite3_reset. The purpose of reset is to allow you to reuse a previous prepared sqlite3_stmt, but you're not doing that here, so reset is unnecessary here and achieves nothing.
At one point in your comments, you said that you received an error "database locked". If you're still getting that having addressed the above items, let us know where you're getting it (at open? during the prepare of a SQL statement?), as there can be different sources of this problem. Certainly multi-threaded database operations can cause this. You need help us diagnose this by showing us precisely which line you're getting this locked message on, and describe what other database operations (beyond the code in your original question) that you might be doing, especially if you're doing anything in a background queue/thread.
The challenge in solving "database locked" problems is that the problem invariably is not in the code in which you're experiencing the "database locked" error, but in some previous database interaction that you failed to handle properly. Technically, a failure to call sqlite3_finalize could cause the "database locked" problem, but in practice that's rarely the case. I see "database locked" errors usually occurring because of other issues, but we really need to know whether you're experiencing it during the opening of the database, or while trying to prepare or step a SQL statement. You need to tell us which for us to go further in the diagnosis. But, like I said, the problem undoubtedly rests in other/prior database interactions, so you might need to share more about what other database operations you're doing prior to the "database locked" error.
If you're interested in a discussion regarding the rewrite of the code in your original question, see below.
A minor suggestion, but I'd suggest altering your initDatabase method to make sure you log an error message if the sqlite3_open call fails, such as:
-(void)initDatabase
{
// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:#"contact1.sqlite"];
// 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 contactList table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS contactList (sapCustId TEXT, sapContactId TEXT, record_ID NUMERIC PRIMARY KEY, timestamp TEXT)";
char *error;
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(#"Database and tables created.");
}
else
{
NSLog(#"%s: error creating table: %s", __FUNCTION__, sqlite3_errmsg(databaseHandle));
}
}
}
else
{
NSLog(#"%s: error opening database: %s", __FUNCTION__, sqlite3_errmsg(databaseHandle));
}
}
Second, you look at insertRecord:
You want to fix the order that you use the four bind statements.
You can also eliminate the redundant if statement.
If you use assertions or return from the middle of a method doing database interactions, you always want to do a graceful cleanup. For example, you'll see if my prepare failed, I'll just close, but if the step failed, then I make sure to finalize and close.
You probably should get rid of those inappropriate release statements, moving them to the right place in the code.
You should change the method signature to have named parameters (because in the General Rules section of the Coding Guidelines, Apple says that we should name our method keywords.
Thus:
- (void) insertRecord:(int)recordID
sapCustId:(NSString *)sapCustId
sapContactId:(NSString *)sapContactId
timestamp:(NSString *)timestamp
{
[self initDatabase];
sqlite3_stmt *statement = nil;
const char *sql_stmt = "INSERT INTO contactList (sapCustId, sapContactId, timestamp, record_ID) VALUES (?, ?, ?, ?)";
if(sqlite3_prepare_v2(databaseHandle, sql_stmt, -1, &statement, NULL) != SQLITE_OK)
{
NSLog(#"%s: insert prepare error: '%s'", __FUNCTION__, sqlite3_errmsg(databaseHandle));
sqlite3_close(databaseHandle);
NSAssert(0, #"insert prepare error");
}
sqlite3_bind_text(statement, 1, [sapCustId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [sapContactId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [timestamp UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 4, recordID);
if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(#"%s: insert step error: '%s'", __FUNCTION__, sqlite3_errmsg(databaseHandle));
sqlite3_finalize(statement);
sqlite3_close(databaseHandle);
NSAssert(0, #"insert step error");
}
sqlite3_finalize(statement);
sqlite3_close(databaseHandle);
}
Try this
You are missing sqlite3_reset, and numbering should start from 1
- (void) updateRecord:(int)recordID:(NSString *)sapCustId:(NSString *)sapContactId:(NSString *)timestamp {
[self initDatabase];
sqlite3_stmt *updateStmt = nil;
if(updateStmt == nil) {
const char *sql_stmt = "update contactList Set sapCustId = ?, sapContactId = ?, timestamp = ? Where record_ID = ?";
if(sqlite3_prepare_v2(databaseHandle, sql_stmt, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(databaseHandle));
}
sqlite3_bind_text(updateStmt, 1, [sapCustId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 2, [sapContactId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 3, [timestamp UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(updateStmt, 4, recordID);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(databaseHandle));
sqlite3_reset(updateStmt);
sqlite3_finalize(updateStmt);
sqlite3_close(databaseHandle);
//Reclaim all memory here.
[sapContactId release];
[sapCustId release];
}
Hope this helps you..
EDIT :-
You have not added sqlite3_finalize(); in any of your previous function and so you are getting database lock error..

When I try SQLite on iOS, INSERT and UPDATE does not work

I createed a table in user.db, this file is in my xcode project folder.
create table user (uid INTEGER PRIMARY KEY ASC AUTOINCREMENT, username, mobile);
Next, in the app delegate .m file, I insert two rows, then I query select * from user and print it.
However, use statement and sqlite3_exec, the insertions are not successful, and there are no errors at all. user.db has 3 rows I inserted from sqlite command line. When I run this app, it prints 5 rows of data, 2 rows are inserted by me, but when I query in sqlite command line, there are still 3 rows, the two newer rows are not committed?
I also run sqlite3_get_autocommit, and find out autocommit=1, so what have I missed to cause this to happen?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
NSString* file = [[NSBundle mainBundle] pathForResource:#"user" ofType:#"db"];
sqlite3* database = NULL;
if(sqlite3_open([file UTF8String], &database) == SQLITE_OK)
{
NSLog(#"database open successfully.");
// stmt
// int autocommit = sqlite3_get_autocommit(database);
sqlite3_stmt* stmt;
static char * sql = "insert into user (username, mobile) values('xxxx', '5555555');commit;";
int success = sqlite3_prepare_v2(database, sql, -1, &stmt, NULL);
if(success != SQLITE_OK)
{
NSLog(#"create stmt failed");
}
success = sqlite3_step(stmt);
if(success != SQLITE_OK)
{
NSLog(#"insert failed");
}
sqlite3_finalize(stmt);
sqlite3_exec(database, "begin;insert into user (username, mobile) values('yyyy', '666666');commit;", NULL, NULL, 0);
// insert
char *zErrMsg = 0;
int rc = sqlite3_exec(database, "select * from user", MyCallback, 0, &zErrMsg);
if(rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
}
}
sqlite3_close(database);
return YES;
}
You can't update databases stored in the app resources. Try copying the database to your app's Document or Library folder and then updating the database there.

Resources