My app has a SQLite database. I don’t know how to explain, but sometimes all tables from the sqlite file gets deleted. It happen frequently. The .db file still exists but no tables.
The tables in an SQLite database (for a given path) will be removed only in these two situations:
The DROP TABLE command was executed against the database
The database file was replaced with a version that did not contain the tables.
Alternatively, the code may be creating a new database or the opening the wrong database file. If there are no tables then a new database/file was probably created. The "relative path" can play a role in where the SQLite file is opened from.
Find out which situation is occurring - and fix it.
Related
I have a database preloaded in SQLite: "BenedictusCoreData.sqlite".
I add it to the project in XCode.
I modify the BenedictusCoreData.xcdatamodeld to create the entities and attributes with the same name and type as the tables and columns of my sqlite preoloaded.
I run the app in the simulator without writing any additional code. I print the main bundle path and go to the sandbox of the application. And inside /Library/Application Support/ I find the three files: .sqlite .sqlite-shm .sqlite-wal
I open the .sqlite and obviously it is empty, however Xcode has created the tables and the columns with capital letters, adding "Z" to the tablenames and "Z" to the column names, appart from adding new tables "Z_METADATA" "Z_MODELCACHE", "Z_PRIMARYKEY".
At this point, I assume that it is not a good idea just to copy my sqlite from the bundle into the "Application Support" folder and rewrite the file, because the structure is totally different.
So I am lost about how to proceed in order to copy a sqlite preloaded database into CoreData the first time you open the app.
May be you can help me.
In the appDelegate you can change the default .xcdatamodel name to the one you added. Then xcode will load your preloaded data .sqlite file instead of the default one. You should search it up there's tutorials on this
I am writing a quiz-style app which is populated from a SQLite database through FMDB. I have copied the database from the app bundle to the app documents directory so that I have read/write privileges and can therefore save user data such as scores to the database as well.
However, I need a way to compare the database in the app documents with the database in the app bundle, so that updates to the bundle db (e.g. new questions) can be copied to the docs db. Simply copying the entire bundle db over to app docs isn't an option as this would overwrite previously saved user data. I had some sort of differences statement in mind such as
SELECT questionID FROM Bundle.Master EXCEPT SELECT questionID FROM AppDocs.Master
to look at what had changed and go from there, but I'm unsure how to use the executeQuery command with more than one db simultaneously.
Any thoughts/alternative approaches appreciated.
You could ship your bundled db with a "version", and copy this version along with other tables in the documents db. On app start up, compare the version of the bundled db with the version of the documents db, and perform a new import if necessary.
You'd have all the opportunities to perform any db migration and (re)import exactly as it needs to be done.
I am creating a Database called Reminder.sqlite via Sqlite Manager add-on in firefox, after creating I am importing it to my iOS Application, the problem is that records are saved in database table but when I run a Select query in Sqlite manager no records are displayed any suggestions why this is happening?
make sure you have add database file into project and remove database file from document directory.
because old file is exist in document director, your new file with same name cannot be overwrite old one.(according to approach, if we use in code)
It may help you.
I am working with sqlite in iOS app. I have created login and register screens and tables successfully. Now, I am creating another table to save data. The table has been created but when I run the query to insert values, I get following error --- "no such table: proposalInfo". Can somebody help me in this ?
If you created database on terminal, then the problem is likely how you got the database you accessed from the terminal to your iOS device. Usually you would include it in the bundle (and make sure you have a checkmark against the appropriate target), and then you'd have code that looks for the database in “Application Support” directory, and if it does not find it, copy it from bundle to there. (Note, in the past, we would have recommended the “Documents” folder, but nowadays that is for user-facing files; we use “Application Support” directory nowadays for files used internally within the app.) The typical problem is simple bug in that logic (perhaps in previous iteration of development), which failed to copy it properly and then subsequently called sqlite3_open which created blank database.
Likely, somewhere in this process, there was insufficient error detection/reporting, so some error went undetected and blank database was created. So, I would recommend:
remove app from device (to get rid of any blank database in “Application Support” folder, if any);
double check error detection/reporting (e.g. check all NSFileManager method return codes and NSError objects);
do not use sqlite3_open, but rather use sqlite3_open_v2 so that it will never create blank database (namely, with SQLITE_OPEN_READWRITE option, but not SQLITE_OPEN_CREATE); and
run app again and see if you can identify where in the process it went awry.
By the way, if you do not believe us that the table is really missing from the database, I would suggest you open the simulator’s database from macOS SQLite tool, and simply confirm. So
navigate to the simulator’s “Application Support” folder:
~/Library/Developer/CoreSimulator/Devices/[GUID]/data/Containers/Data/Application/[GUID]/Library/Application Support
And
Open that database in your SQLite terminal interface, and confirm existence or absence of the table in question.
Please create table using SQLite browser. Which is you can download from here.
Then follow below step
1) Open your project db by clicking.
2) Create table Query or using UI.
3) Execute query whatever you want.
I have some general questions about iphone app updates that involves sqlite db.
With the new update does the existing sqlite db get overwritten with a copy of the new one?
If the update doesn't involve any schema changes then the user should be able to reuse the existing database with their saved data, right? (if the existing database doesn't get overwritten from 1 above )
If there are some schema changes, what's the best way to transfer data from the old database into the new one? Can some one please give me guidelines and sample code?
Only files inside the app bundle are replaced. If the database file is in your app's Documents directory, it will not be replaced. (Note that if you change files inside your app bundle, the code signature will no longer be valid, and the app will not launch. So unless you are using a read-only database, it would have to be in the Documents directory.)
Yes.
What's best depends on the data. You're not going to find sample code for such a generic question. First, you need to detect that your app is running with an old DB version. Then you need to upgrade it.
To check versions:
You could use a different file name for the new schema. If Version2.db does not exist but Version1.db does, do an upgrade.
You could embed a schema version in your database. I have a table called metadata with a name and value column. I use that to store some general values, including a dataversion number. I check that number when I open the database, and if it is less than the current version, I do an upgrade.
Instead of creating a table, you could also use sqlite's built-in user_version pragma to check and store a version number.
You could check the table structure directly: look for the existence of a column or table.
To upgrade:
You could upgrade in place by using a series of SQL commands. You could even store a SQL file inside your app bundle as a resource and simply pass it along to sqlite3_exec to do all the work. (Do this inside a transaction, in case there is a problem!)
You could upgrade by copying data from one database file to a new one.
If your upgrade may run a long time (more than one second), you should display an upgrading screen, to explain to the user what is going on.
1) The database file isn't stored as part of the app bundle so no, it won't get automatically overwritten.
2) Yes - all their data will be saved. In fact, the database won't get touched at all by the update.
3) This is the tricky one - read this fantastically interesting document - especially the part on lightweight migration - if your schema changes are small and follow a certain set of rules, they will happen automatically and the user won't notice. however, if ther are major changes to the schema you will have to write your own migration code (that's in that links as well)
I've always managed to get away with running lightweight migrations myself - it's by far easier than doing it yourself.
What I do is that I create a working copy of the database in the Documents directory. The main copy comes with the bundle. When I update the app I then have the option to make a new copy over the working copy, or leave it.