Copy sqlite db into Android app

JamesHeaneyJamesHeaney James HeaneyGBMember
edited June 2013 in Android

Please help. I cannot get my sqlite DB to copy into my Android app. I have done this without issue for a iOS app but the android path seems sooo much harder!

I have the sqlite DB (eng.sqlite) in the root folder of my project. I have tried "Content" build actions (like in the iOS app), AndroidResource, AndroidAsset, EmbeddedResource but I still get "File not found" when i try to copy my DB into the "databases" folder.

I have also tried putting the db in the Assets folder and then tried every build option but still cannot find the file!

string db = "/data/data/SpecDroid.SpecDroid/databases/eng.sqlite"; 

string rootDbPath = "/data/data/SpecDroid.SpecDroid/eng.sqlite"; 

//rootDbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "eng.sqlite"); <-- DIDNT WORK

if(File.Exists(db) == false) {
    Console.WriteLine("Installing DB!");
File.Copy(rootDbPath, db);

}

Any ideas??

All i want to do is copy a previously built sqlite db into the databases folder and then connect to it.

Thanks

Posts

  • JamesHeaneyJamesHeaney James Heaney GBMember

    Yep - he had the same problem too. No solution tho.

  • StevenTheEvenStevenTheEven Michael Andersson SEMember ✭✭✭

    Huh? Read more then one comment.

  • HarshadcseHarshadcse Harshad Patel USMember ✭✭

    hii..@JamesHeaney

    You can try like in below Way :

    db = /data/data/Sqlite_Simple.Sqlite_Simple/files/test.db

    1) Open the Android-sdk-windows

    2) then open the tools

    3) then ddms open

    4) Dalvik Debug Monitor is open

    5) then Device -> File Explorer.

    6) data/data/Sqlite_Simple.Sqlite_Simple/files/test.db

    7) test.db copy and open sqlite manager ...

    Thanks!!!!Harshad

    5.png
    824 x 632 - 118K
    6.png
    1366 x 768 - 208K
  • BenLuBenLu Ben Lu TWMember
  • BenLuBenLu Ben Lu TWMember

    @JamesHeaney Hi James, Did you resolve this issue? In my project,just like you. I have a SQLite DB,I tried to use Path.GetFullPath to get path of my SQLite DB and "Content" build actions.But I can't do file.copy or file.move....so did you find a solution to resolve this issue?

  • MannyDiazMannyDiaz Manny Diaz USMember

    I'm getting a access denied when I try to copy the sqlite database from the Assets to /data/data/Android.Android/files/

    `string DB_PATH = "/data/data/Android.Android/files/"; string DB_NAME = "database";

        private void copyDataBase()
        {
            using (var assets = Assets.Open(DB_NAME))
                using (var dest = File.Create (DB_PATH))
                    assets.CopyTo (dest);
        }`
    

    Any help would be appreciated.

  • JohnMurrayJohnMurray John Murray GBMember
    edited November 2013

    Manny - see my thread 'newbie question' more recently In short - if you put the sqlite in the assets folder of the project you have to open it as astream and write it to the \data\com.myapp\files folder on your device this is some example code for 'copy2dtabase which workss for me.

    // copy database
                Stream iStream = Assets.Open("locair.db3");
                oStream = new FileOutputStream(outFilePath);
                byte[] buffer = new byte[2048];
                int length = 2048;
                //    length = Convert.ToInt16(length2);
    
    
    
                while (iStream.Read(buffer, 0, length) > 0)
                {
                    oStream.Write(buffer, 0, length);
                }
                oStream.Flush();
                oStream.Close();
                iStream.Close();
    
  • ASowerbyASowerby William Andrew Sowerby GBMember

    I use the following code as suggested by Jonathan Prior:

    private void copyAssetDataBase()  
            {
    
                using (var source = Assets.Open(@"app.db"))
                using (var dest = OpenFileOutput("app.db", FileCreationMode.WorldReadable | FileCreationMode.WorldWriteable))
                {
                    source.CopyTo(dest);
                }
            }
    
  • rabih.7535rabih.7535 rabih USMember

    any update regarding the upload of sql lite db to a real android device?

  • ASowerbyASowerby William Andrew Sowerby GBMember

    Well, we provide ADB push/pull and Wi-Fi file transfer options. The Wi-Fi option uses Apple's Bonjour for Windows with sockets on the PC and NSD Manager on the Android.

Sign In or Register to comment.