java - How to I package my database in with my app? -
I have a SQLite database which is essential in my application. When I run it in an emulator on my PC then it flawlessly Works when I run it on my phone then it crashes because this method is empty because it can not connect to the database.
this is where I Have prepared their database in preparation. I am unsure of how I want to package it with my application.
Here's my database handler. Java:
package com. Examples. my application; Import android.accounts.Account; Import android.database.DatabaseUtils; Import android.content.ContentValues; Import android.content.Context; Import android.database.Cursor; Import android.database.sqlite.SQLiteDatabase; Import android.database.sqlite.SQLiteOpenHelper; Import android.net.Uri; Static android.database.DatabaseUtils.dumpCursorToString import; / ** * Broadband created by Brad / * 19/07/2014 * / Public Class Databasendler Extended SQLiteOpenHelper {Personal Static Final End DATABASE_VERSION = 1; Private static last string DATABASE_NAME = "cargrid", TABLE_CARS = "car", KEY_ID = "id", KEY_POSTCODE = "postcode", KEY_ADDRESS = "address", KEY_IMAGE = "image"; Public databashendler (reference reference) {super (references, database, ANN, blank, databasebessons); } @Override Public Zero OnCreate (SQLiteDatabase Database) {db.execSQL (+ TABLE_CARS + "(" + KEY_ID + "integer primary key autoincrement," + KEY_ADDRESS + "text," "create table" + KEY_POSTCODE + "text" + KEY_IMAGE + "TEXT)"); } @Override Public Zero onUpgrade (SQLiteDatabase db, integer oldVersion, integer NewVersion) {db.execSQL ("Drop table if exists" + TABLE_CARS); OnCreate (database); } Public Zero Car Car (Car Car) {SQLiteDatabase db = getWritableDatabase (); Material values = new material value (); Values.put (KEY_ADDRESS, car .get_address ()); Values.put (KEY_POSTCODE, car.get_postcode ()); Values.put (KEY_IMAGE, car .get_image ()); Db.insert (TABLE_CARS, zero, value); Db.close (); } Public car getCar (id) {SQLiteDatabase db = getReadableDatabase (); Cursor cursor = db.query (TABLE_CARS, new string [] {KEY_ID, KEY_IMAGE, KEY_ADDRESS, KEY_POSTCODE}, KEY_ID + "=?", New string [] {String.valueOf (id)}, null, null, null, null ); If (cursor! = Null) cursor.movatofst (); Car car = new car (integer parasont (cursor .getString (0)), cursor.text string (1), cursor.gate string (2), cursor.txt string (3)); Return car; } Public int getCarCount () {// SELECT * from SQLiteDatabase db = getReadableDatabase (); Cursor cursor = db.rawQuery ("SELECT * FROM" + TABLE_CARS, blank); Cursor.close (); Return cursor .getCount (); } Public string getRandomImageKey () {// SELECT * from cars SQLiteDatabase db = getReadableDatabase (); Select "" by cursor cursor = db.rawQuery ("+ TABLE_CARS +, null" order random () range 1 "+ KEY_ID +", "+ KEY_IMAGE +); Int columnIndex = cursor.getColumnIndex (KEY_ID); String Tourtrain = CursorGet String (Column Index); Return to return; } Get Public Car Content () {SQLiteDatabase db = getWritableDatabase (); Ordering range ("Range") by "+ TABLE_CARS +" to "+ KEY_IDDRESS +", "+ KEY_POSTCODE +", "+ KEY_IMAGE +", "String SQL =" select "+ KEY_ID +"; Cursor cursor = db.rawQuery (SQL, new string [] {}); Car car = null; Try {if (cursor.moveToFirst ()) {car = new car (Integer.parseInt (cursor.getString (0)), cursor.getString (1), cursor.getString (2), cursor.getString (3)); }} Finally {if (cursor! = Null & amp; cursor.iciclast ()) {cursor.close (); } Db.close (); } Return car; }}
How do I package my database with my app?
To open your database from the property on the first part of your app.
Comments
Post a Comment