0

I have successfully Create a database program. Basically i am dealing with a little complex database, So need to make database in SQLITE MANAGER and Import/link that Database with my android code. Please suggest me Tutorial to do this. Or any other Piece of code that help me. Thanks

here Is my code for database now i want to Import mYdataBase.sqlite file in that.

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contactsManager";
    private static final String TABLE_CONTACTS = "contacts";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }
makvalti
  • 87
  • 1
  • 9
Zeeshan Chaudhry
  • 842
  • 2
  • 15
  • 31

2 Answers2

5

if you want import your database file from asset folder than below code can help you

void checkDB() throws Exception {
        try {
            SQLiteDatabase dbe = SQLiteDatabase
                    .openDatabase(
                            "/data/data/com.yourpackagename/databases/yourfile.sqlite",
                            null, 0);
            Log.d("opendb", "EXIST");
            dbe.close();
        } catch (Exception e) {

            AssetManager am = getApplicationContext().getAssets();
            OutputStream os = new FileOutputStream(
                    "/data/data/com.yourpackagename/databases/yourfile.sqlite");
            byte[] b = new byte[100];

            int r;
            InputStream is = am.open("yourfile.sqlite");
            while ((r = is.read(b)) != -1) {
                os.write(b, 0, r);
            }
            Log.i("DATABASE_HELPER", "Copying the database ");
            is.close();
            os.close();
        }
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
0

try using this http://codinglookseasy.blogspot.in/2012/08/sqlite-database.html. This a sample for using the database in your application. hope this helps you

G_S
  • 7,068
  • 2
  • 21
  • 51
  • Thanks; But its not what i really wants. i have a database file (*.sqlite) which i create in SQLITE MANAGER, Now i need to connect that database with my android code – Zeeshan Chaudhry Sep 05 '12 at 17:50
  • well take a look at Hardik Nadiyapara's answer. I think that's the one you need – G_S Sep 05 '12 at 17:52
  • 2
    no problems. Even i got to know a new concept after this thread. Thanks to you too – G_S Sep 05 '12 at 17:54