'Working with .sql file in android?
I am working on android project. In my project I have a 'DatabaseFood .sql 'file(initially it was a .csv file), it looks like this
BEGIN TRANSACTION;
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO `android_metadata` VALUES ('en_US');
CREATE TABLE "DBfood" (
`SNo.` INTEGER,
`Food` TEXT,
`Calories` INTEGER,
`Protein` INTEGER,
`Fats` INTEGER,
`Carbs` INTEGER,
`Fibers` INTEGER);
INSERT INTO `DBfood` VALUES (1,'Apple',120,50,40,30,20);
INSERT INTO `DBfood` VALUES (2,'Banana',111,45,40,50,21);
INSERT INTO `DBfood` VALUES (3,'Orange',91,31,33,19,21);
INSERT INTO `DBfood` VALUES (4,'Grapes',110,41,11,14,13);
INSERT INTO `DBfood` VALUES (5,'Mango',150,51,12,41,53);
COMMIT;
Now my questions are -
Where do i put this file if i plan to have about 20000 entries in it. In res/raw folder or res/assets folder or somewhere else.
How I could use all the data in the file. For ex - If i want to get the calories of apple or If i want to get proteins of Orange.
How could i perform arithmetic operations with the properties(Calorie, protein etc) of food.
Please suggest me answers to my questions with sample code or references. Also, If you know how to match this file with 'DatabaseHelper.java'(for ex) class in which we create tables and perform CRUD operations and execute queries.
But remember that it would always be happening with a very large amount of data. I have only put that code so that it becomes easy to answer with sample code.
Solution 1:[1]
I suggest you read this post: http://www.vogella.com/tutorials/AndroidSQLite/article.html#loader_cursorloader
It explains in detail how to handle SQL operations in an android app and also contains lots of code samples. It should answer most of your questions.
Once you've created the database on first launch of your application, you can perform any operation SQL offers, including arithmetics.
As for where to put the .sql file: You can put it in the /res/assets folder and acces it via
context.getAssets().open("my_sql_file.sql");
Good luck!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Richard R |