Home > Mobile >  Ndroid SQLite database problems
Ndroid SQLite database problems

Time:04-01

I try to use SQLite configuration database code, but prompt return is an empty, help to analysis the reason, the part of the code is as follows, program in SQLiteDatabase db=dbHelper. GetWritableDatabase (); Return an error from the log, is not to create the database, but the local database. So where is not wrong.


The Activity code is as follows:

Public void initOpen () {

/* *
* create a database
*/
System. The out. Println (" SQLite - initOpen write data: -- -- -- -- -- -- -- -- -- ");
AmeOpenHelper dbHelper=new AmeOpenHelper (DataActivity. This "amedb. Db", null, 1);
//get a readable SQLiteDatabase object static
SQLiteDatabase db=dbHelper. GetReadableDatabase ();

}


Public void initEvetn () {

AmeOpenHelper dbHelper=new AmeOpenHelper (DataActivity. This "amedb. Db", null, 1);
Write data System. Out. Println (" SQLite: -- -- -- -- -- -- -- "+ dbHelper);
//get a readable SQLiteDatabase object
SQLiteDatabase db=dbHelper. GetWritableDatabase ();
ContentValues CV=new ContentValues ();
CV. Put (" mtype ", idm);
//call the insert method, insert data into database
The insert (" ame_step_table ", null, CV);
}


SQLiteOpenHelper code below


Public class AmeOpenHelper extends SQLiteOpenHelper {

Private static final String TAG="AmeOpenHelper";
Public static final int VERSION=1;
//define the database file name
Public static final String DB_NAME="amedb. Db";


/* *
* @ param context
* @ param name the name of the database file
* @ param factory null
* @ param version of the database file version
*/
Public AmeOpenHelper (Context Context, String name, SQLiteDatabase. CursorFactory factory, int version) {
Super (context, name, factory, version);

System. The out. Println (" AmeSQLite connect to the database: -- -- -- "+ context +" -- "+ name +" -- "+ factory +" -- "+ version);


}

//provides the constructor
Public AmeOpenHelper Context (Context) {
//call the class of private constructor
Super (context, DB_NAME, null, VERSION);
}

//callback method when create the data for the first time
@ Override
Public void onCreate (SQLiteDatabase db) {
The d (TAG, "onCreate");
System. The out. Println (" AmeSQLite set up database: -- -- -- -- -- -- -- -- -- ");
//String SQL="create table ame_step_table (id int, sname varchar (20), sage int, ssex varchar (10), num int)";
//create a database table statement

Db. ExecSQL (" create table IF NOT EXISTS ame_step_table "+
"(id integer primary key autoincrement, mtype int");

}

//when the database upgrade is a callback method
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
The d (TAG, "onUpgrade: oldVersion" + + "newVersion=" + newVersion oldVersion);
String SQL="alter table t_user add c_money float";
Db. ExecSQL (SQL);
}

//callback when the database is open the method
@ Override
Public void onOpen (SQLiteDatabase db) {

The d (TAG, "onOpen");
}

}

The error log is as follows:
D/HwCust: Create obj success use class. Android app. HwCustActivityImpl
E/CursorResourceWrapper: context is wrong!
I/System. Out: AmeSQLite connect to the database: - com. Heartrate. Utils. DataActivity @ 623 e598 -- amedb. Db - null - 1
SQLite write data: -- -- -- -- -- -- -- com. Heartrate. Utils. AmeOpenHelper @ 8 cd41f1
D/AndroidRuntime: Shutting down the VM
E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Heartrate. Activity, PID: 21060
Java. Lang. RuntimeException: Error identifiers broadcast Intent {act=com. Ainia. Ble. RETURN_BLE_DATA FLG=0 x10 (from the extras)} in the heartrate. Fragments. StepCounterFragment $2 @ ebe1e72
An android. App. LoadedApk $ReceiverDispatcher $Args. Lambda $- android_app_LoadedApk $$Args_58300 ReceiverDispatcher (LoadedApk. Java: 1448)
An android. App. - $$FilBqgnXJrN9Mgyks1XHeAxzSTk Lambda. $m $0 (Unknown Source: 4)
An android. App. - $Lambda $FilBqgnXJrN9Mgyks1XHeAxzSTk. Run (Unknown Source: 0)
An android. OS. Handler. HandleCallback (Handler. Java: 808)
An android. OS. Handler. DispatchMessage (Handler. Java: 101)
. An android OS. Stars. Loop (166). Which Java:
. An android app. ActivityThread. Main (7529) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. Zygote $MethodAndArgsCaller. Run (Zygote. Java: 245)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 921)
Under Caused by: Java. Lang. NullPointerException: Attempt to invoke virtual method 'android. The database, sqlite SQLiteDatabase android. The content. Context. OpenOrCreateDatabase (Java. Lang. String, int, android. The database, sqlite. SQLiteDatabase $CursorFactory, android. Database. DatabaseErrorHandler)' on a null object reference
An android. Content. ContextWrapper. OpenOrCreateDatabase (ContextWrapper. Java: 299)
An android. Database. Sqlite. SQLiteOpenHelper. GetDatabaseLocked (SQLiteOpenHelper. Java: 266)
An android. Database. Sqlite. SQLiteOpenHelper. GetWritableDatabase (SQLiteOpenHelper. Java: 206)
At com. Heartrate. Utils. DataActivity. InitEvetn (DataActivity. Java: 187)
At com. Heartrate. Utils. DataActivity. AddList (DataActivity. Java: 132)
At com. Heartrate. My. MeasurementData. SetMeasurementData (MeasurementData. Java: 283)
At com. Heartrate. Fragments. StepCounterFragment $2. OnReceive (StepCounterFragment. Java: 59)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related