0

I am trying to develop android application (first time), so I'm using SQLite to store data. But every time I run the application all data are null! I think the problem is when I run the application it recreates all the tables and they come as new empty tables!

Can anyone help or give example how to check if the tables are already created, and if yes copy the data, if not create and that means it's a first time! How can I store data for a long time by using SQLite? If I cant what is the best way to store data?

        //food
        private static final String food= "food";
        //---- columns ----- 
        private static final String fid = "fid";
        private static final String fname= "fname";
        private static final String category = "category";
        private static final String quantity= "quantity";
        private static final String unit= "unit";
        private static final String carbs= "carbs";

        //food
            private static final String DATABASE_CREATE_Food="CREATE TABLE "
        +food+" ("+fid
        + " INTEGER PRIMARY KEY AUTOINCREMENT , "
        +fname+ " TEXT NOT NULL, "
        +category+" TEXT Not NULL, "
        +quantity+" Double Not Null, "
        +carbs+" Integer NOT NULL, "
        +unit+" TEXT NOT NULL" 
        +");";



public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE_Food);

    }
    /** Called when the DATABASE_VERSION is changed to higher version */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MyAppDB.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");

        db.execSQL("DROP TABLE IF EXISTS" + food);

        onCreate(db);
    }
jory
  • 41
  • 2
  • 4
  • 3
    The tables shouldn't be recreated every single time. You should post your code so a reason for the recreation can be pinpointed. – A--C Mar 18 '13 at 00:11

1 Answers1

0

There are a lot of ways to store data in android, SQLite is one of them.
There is probably something wrong with your code, because it shouldn't be null everytime you start the app. Post your code here, so someone can find out what's wrong with it.

For other ways to store data in android, check out this link from developer.android site
http://developer.android.com/guide/topics/data/data-storage.html

Reda
  • 439
  • 1
  • 5
  • 15
  • thanks for help .. but someone told i cant use SQLite to store data for a long time ? Also i want the data to be locally in the device " no internet needs "! can you suggest any kind of DataBase must or butter user ? – jory Mar 18 '13 at 09:52