-4

How can I connect my sqlite database in android set in a folder in "/SDCard/myapp"

Thanks

3 Answers3

0

Try this This class provides developers with a simple way to ship their Android app with an existing SQLite database

Mahmoud.M
  • 174
  • 11
0

rquest permission youself.

The way to get db:

package com.airi.buyue.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.airi.buyue.BuyueApp;
import com.airi.buyue.R;
import com.airi.buyue.util.FileUtils;
import com.airi.buyue.util.SystemUtils;
import com.airi.buyue.util.Utilities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataManager {

    public static final String DB_NAME = "zonen.db";
    public static final String TABLE_NAME = "m_nzone_copy";

    private SQLiteDatabase database;
    private Context context;

    public DataManager(Context context) {
        this.context = context;
        getPath();
    }

    public SQLiteDatabase getDatabase() {
        return database;
    }

    public void setDatabase(SQLiteDatabase database) {
        this.database = database;
    }

    public void openDatabase() {
        this.database = this.openDatabase(getPath() + "/" + DB_NAME);
    }

    public  String getPath(){
        String DATABASE_PATH;
            DATABASE_PATH = Environment.getExternalStorageDirectory().getPath()+"/"+"myapp";
        return DATABASE_PATH;
    }
    private SQLiteDatabase openDatabase(String dbfile) {
        SystemUtils.debugLog("test-zonedb","attempt");
        try {
            if (!(new File(dbfile).exists())) {
                //do sth
            }
            FileUtils.updateFile(dbfile);
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);
            return db;

        } catch (FileNotFoundException e) {
            SystemUtils.debugLog("test-zonedb","file no found");
            SystemUtils.attemptPrintError(e);
        } catch (IOException e) {
            SystemUtils.debugLog("test-zonedb","io expection");
            SystemUtils.attemptPrintError(e);
        }
        return null;
    }

    public void closeDatabase() {
        if(this.database!=null){
            this.database.close();
        }
    }
}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
0

Define in your class :

public static SQLiteDatabase database;
public static final String   DIR_SDCARD   = Environment.getExternalStorageDirectory().getAbsolutePath(); //adress SD CARD ro migire
public static final String   DIR_DATABASE = DIR_SDCARD + "/myapp/"; 

Then, write in your onCreate :

context = this.getApplicationContext();
new File(DIR_DATABASE).mkdirs(); 

for creating Databse :

database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/myapp", null); 

For crating table in databse :

    database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/myapp.sqlite", null); //database ro misaze
    database.execSQL("CREATE  TABLE  IF NOT EXISTS person (" +
            "person_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , " +
            "person_name TEXT, " +
            "person_family TEXT, " +
            "person_age INTEGER" +
            ")")

for insert into database :

        for (int i = 0; i < 20; i++) {
        database.execSQL("INSERT INTO person (person_name,person_family,person_age) VALUES ('Name#" + i + "','Family#" + i + "'," + i + ")");
    }

for read from database :

    Cursor cursor = database.rawQuery("SELECT * FROM person WHERE person_age>10", null);
    while (cursor.moveToNext()) { //age betoone bere be badi, ta zamani ke cursor ee vojud dashte bashe in karo mikone
        String name = cursor.getString(cursor.getColumnIndex("person_name"));
        String family = cursor.getString(cursor.getColumnIndex("person_family"));
        int age = cursor.getInt(cursor.getColumnIndex("person_age"));
        Log.i("LOG", "Record: " + name + " " + family + ", " + age);
    }
    cursor.close();

for delete and update :

    database.execSQL("DELETE FROM person WHERE person_age>10");
    database.execSQL("UPDATE person SET person_name='test' WHERE person_age>7");
BobyCloud
  • 244
  • 1
  • 4
  • 11