1

In my Android project I have a DatabaseFood.sql file:

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;

Where do I put this file if I plan to have about 20,000 entries in it? In res/raw folder or res/assets folder or somewhere else? How could I use the data in the file if I want to get the calories of an apple or proteins of an orange? How could I perform arithmetic operations with the properties (Calories, Protein etc.) of food?

Please answer with sample code or references. Also, how to match this file with 'DatabaseHelper.java' class in which I create tables and perform CRUD operations and execute queries? It would be happening with a very large amount of data (I only put that code so it becomes easy to answer with sample code).

user4157124
  • 2,809
  • 13
  • 27
  • 42
  • 1
    Here's an answer that shows how to parse/exec a .sql file... it does assume that each statement is on a single line though: http://stackoverflow.com/a/8199589/1715829 – Buddy May 10 '15 at 14:53
  • possible duplicate of [Using .sql file in android](http://stackoverflow.com/questions/30150070/using-sql-file-in-android) – 2Dee May 20 '15 at 10:21

1 Answers1

0

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!

Richard R
  • 873
  • 6
  • 20