Open sqlite file java

Android — How to Use SQLiteDatabase.open?

to create/open a database instead of making use of the SQLiteOpenHelper (because i want to pass in the flag SQLiteDatabase.NO_LOCALIZED_COLLATORS . However, i am getting this exception for that line of code:

04-18 09:50:03.585: ERROR/Database(3471): sqlite3_open_v2("/data/data/edwin11.myapp/databases/myapp.db", &handle, 6, NULL) failed 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): java.lang.RuntimeException: An error occured while executing doInBackground() 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.os.AsyncTask$3.done(AsyncTask.java:200) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.FutureTask.run(FutureTask.java:122) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.lang.Thread.run(Thread.java:1060) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): Caused by: android.database.sqlite.SQLiteException: unable to open database file 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1584) 04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638) . 

Doing some testing just before that line of code (using File.isExists ) shows that the file /data/data/edwin11.myapp/databases/myapp.db does not exist. Would that be the cause of the error? (Or am i just using SQLiteDatabase.openDatabase the wrong way?) Would it help if i create the file beforehand? (Shouldn’t that be taken care of by the SQLiteDatabase.CREATE_IF_NECESSARY flag that i passed in?) If creating the file manually is the way to go, is it just an empty file, or do i have to write something to it? Thanks and Regards.

Источник

Open sqlite file java

Main class wrapping an SQLite database.

Field Summary
protected int error_code
Internal last error code for exec() methods.
protected long handle
Internal handle for the native SQLite API.
Constructor Summary
Database ()
Method Summary
private static void _backup (Backup b, Database dest, String destName, Database src, String srcName)
private void _busy_handler (BusyHandler bh)
private void _busy_timeout (int ms)
private long _changes ()
private void _close ()
private static boolean _complete (String sql)
private void _create_aggregate (String name, int nargs, Function f)
private void _create_function (String name, int nargs, Function f)
private int _db_status (int op, int[] info, boolean flag)
protected static boolean _enable_shared_cache (boolean onoff)
Enable/disable shared cache mode (SQLite 3.x only).
private String _errmsg ()
private void _exec (String sql, Callback cb)
private void _exec (String sql, Callback cb, String[] args)
private void _finalize ()
private void _function_type (String name, int type)
private void _interrupt ()
private void _key (byte[] ekey)
private long _last_insert_rowid ()
private void _open_aux_file (String filename)
private void _open_blob (String db, String table, String column, long row, boolean rw, Blob blob)
Internal SQLite open blob method.
private void _open (String filename, int mode)
private void _open4 (String filename, int mode, String vfs, boolean ver2)
private void _profile (Profile pr)
private void _progress_handler (int n, ProgressHandler p)
private void _rekey (byte[] ekey)
private void _set_authorizer (Authorizer auth)
private void _set_encoding (String enc)
private static int _status (int op, int[] info, boolean flag)
private void _trace (Trace tr)
Backup backup (Database dest, String destName, String srcName)
Initiate a database backup, SQLite 3.x only.
void busy_handler (BusyHandler bh)
Establish a busy callback method which gets called when an SQLite table is locked.
void busy_timeout (int ms)
Set the timeout for waiting for an SQLite table to become unlocked.
long changes ()
Return the number of changed rows for the last statement.
void close ()
Close the underlying SQLite database file.
Vm compile (String sql)
Compile and return SQLite VM for SQL statement.
Vm compile (String sql, String[] args)
Compile and return SQLite VM for SQL statement.
static boolean complete (String sql)
See if an SQL statement is complete.
void create_aggregate (String name, int nargs, Function f)
Create aggregate function.
void create_function (String name, int nargs, Function f)
Create regular function.
int db_status (int op, int[] info, boolean flag)
Return information on SQLite connection status.
String dbversion ()
Return SQLite version number as string.
String error_message ()
Return last error message of SQLite3 engine.
static String error_string (int error_code)
Return error string given SQLite error code (SQLite2).
void exec (String sql, Callback cb)
Execute an SQL statement and invoke callback methods for each row of the result set.
void exec (String sql, Callback cb, String[] args)
Execute an SQL statement and invoke callback methods for each row of the result set.
protected void finalize ()
Destructor for object.
void function_type (String name, int type)
Set function return type.
TableResult get_table (String sql)
Convenience method to retrieve an entire result set into memory.
TableResult get_table (String sql, int maxrows)
Convenience method to retrieve an entire result set into memory.
TableResult get_table (String sql, int maxrows, String[] args)
Convenience method to retrieve an entire result set into memory.
TableResult get_table (String sql, String[] args)
Convenience method to retrieve an entire result set into memory.
void get_table (String sql, String[] args, TableResult tbl)
Convenience method to retrieve an entire result set into memory.
private static void internal_init ()
Internal native initializer.
void interrupt ()
Abort the current SQLite operation.
boolean is3 ()
Check type of open database.
static double julian_from_long (long ms)
Make julian date value from java.lang.Date
void key (byte[] ekey)
Specify key for encrypted database.
void key (String skey)
Specify key for encrypted database.
int last_error ()
Return the code of the last error occurred in any of the exec() methods.
long last_insert_rowid ()
Return the row identifier of the last inserted row.
static long long_from_julian (double d)
Make long value from julian date for java.lang.Date
static long long_from_julian (String s)
Make long value from julian date for java.lang.Date
void open_aux_file (String filename)
Open SQLite auxiliary database file for temporary tables.
Blob open_blob (String db, String table, String column, long row, boolean rw)
Open an SQLite3 blob.
void open (String filename, int mode)
Open an SQLite database file.
void open (String filename, int mode, String vfs)
Open an SQLite database file.
void open (String filename, int mode, String vfs, boolean ver2)
Open an SQLite database file.
Stmt prepare (String sql)
Prepare and return SQLite3 statement for SQL.
void profile (Profile pr)
Set profile function.
void progress_handler (int n, ProgressHandler p)
Establish a progress callback method which gets called after N SQLite VM opcodes.
void rekey (byte[] ekey)
Change the key of a encrypted database.
void rekey (String skey)
Change the key of a encrypted database.
void set_authorizer (Authorizer auth)
Set authorizer function.
void set_encoding (String enc)
Set character encoding.
protected void set_last_error (int error_code)
Internal: set error code.
static int status (int op, int[] info, boolean flag)
Return information on SQLite runtime status.
private void stmt_prepare (String sql, Stmt stmt)
Internal SQLite3 prepare method.
void trace (Trace tr)
Set trace function.
static String version ()
Return SQLite version number as string.
private void vm_compile_args (String sql, Vm vm, String[] args)
Internal compile method, SQLite 3.0 only.
private void vm_compile (String sql, Vm vm)
Internal compile method.
Читайте также:  Java streams flatmap example
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

error_code


handle


Database

Источник

Open downloaded SQLite Database

I have a pre-made SQLite database that I am downloading from the net via an AsyncTask. It downloads the file and stores it on the sdcard in /data/databases/ I have checked the database file and it is successfully downloading and has all the appropriate tables and data but every time I try and open the database and display the stored data I get the following

 03-19 18:43:10.204: E/AndroidRuntime(3057): java.lang.RuntimeException: Unable to start activity ComponentInfo: android.database.sqlite.SQLiteException: no such table: beers: , while compiling: SELECT * FROM beers ORDER BY _id 

which makes no sense because the table is there maybe my databasehelper class is wrong or I am calling it wrong. here is my database.java

 package com.ondrovic.downloader; import java.io.File; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Environment; public class Database extends SQLiteOpenHelper < //File rDIR = Environment.getExternalStorageDirectory(); private static String DBPATH = "/data/databases/BOOMBOZZ/"; private static String DBNAME = "boombozz.db"; private static int DBVER = 1; private SQLiteDatabase db; private final Context dbContext; public Database(Context context) < super(context, DBNAME, null, DBVER); this.dbContext = context; >public void open() < String myPath = DBPATH + DBNAME; db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory() + myPath, null, SQLiteDatabase.OPEN_READWRITE); >public synchronized void close() < db.close(); super.close(); >@Override public void onCreate(SQLiteDatabase db) < // TODO Auto-generated method stub >@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) < // TODO Auto-generated method stub >> 
 db = (new Database(this)).getWritableDatabase(); 

Источник

How to connect SQLite with Java?

You need to have a SQLite JDBC driver in your classpath.

Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don’t need to configure them separately.

i am using this driver, and works fine. But why it is not compacting database while creating.With FireFox sqlite plugin Database compacted from 11.5MB to 11.3MB. how to do that with java.

@Dyapa You need to periodically run VACUUM ; since JDBC doesn’t actually interpret the SQL (for very good reasons) just send it over. Or use a pragma per connection to run in auto-vacuum mode.

For those experiencing this issue with Tomcat this jar needs to be put into the lib folder for respective Tomcat version. Tomcat will also need to be restarted. In the web application, the jar needs to be placed into the WEB-INF lib folder as well. You don’t need to add it to the build path because putting it in the WEB-INF lib folder places it there automatically.

If you are using netbeans Download the sqlitejdbc driver Right click the Libraries folder from the Project window and select Add Library , then click on the Create button enter the Library name (SQLite) and hit OK

You have to add the sqlitejdbc driver to the class path , click on the Add Jar/Folder.. button and select the sqlitejdbc file you’ve downloaded previously Hit OK and you are ready to go !

I struggled a lot because of this problem. I am a Java newbie and using Netbeans. It’s been a frustration. Finally, your answer saved my day.

If you are using Netbeans using Maven to add library is easier. I have tried using above solutions but it didn’t work.

I have added Maven dependency and java.lang.ClassNotFoundException: org.sqlite.JDBC error gone.

Источник

Оцените статью