Reputation: 91
I am create a app for insert data in database and retrieve data form database . When first time activity start then data insert into database and retrieve data from database but when I restart activity and click on button to retrieve data from database than my app close with error..Pleas help me ??
DataBaseHelper class
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(DetailDataBaseAdapter.DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}
}
DataBaseAdapter class
public class DetailDataBaseAdapter{
static final String DATABASE_NAME = "phone.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"PHONE"+
"( " +"ID"+" integer primary key autoincrement,"+ "NUMBER text,SIM text,IMEI text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public DetailDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DetailDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String number,String sim,String imei)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("NUMBER", number);
newValues.put("SIM",sim);
newValues.put("IMEI",imei);
// Insert the row into your table
db.insert("PHONE", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public String getData(String number)
{
Cursor cursor=db.query("PHONE", null, " NUMBER=?", new String[]{number}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String sim_id= cursor.getString(cursor.getColumnIndex("NUMBER"));
cursor.close();
return sim_id;
}
}
MainActivity class
public class MainActivity extends Activity{
TextView tv;
String telNumber,simID,IMEI;
Button btn,bt;
TelephonyManager tm;
DetailDataBaseAdapter detailDataBaseAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
btn = (Button) findViewById(R.id.ok);
detailDataBaseAdapter=new DetailDataBaseAdapter(this);
detailDataBaseAdapter=detailDataBaseAdapter.open();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simID = tm.getSimSerialNumber();
if (simID != null) {
tv=(TextView)findViewById(R.id.tv_sim_no);
tv.setText(simID);
}
// ---get the phone number---
telNumber = tm.getLine1Number();
if (telNumber != null) {
tv=(TextView)findViewById(R.id.tv_phone_no);
tv.setText(telNumber);
}
// ---get the IMEI number---
IMEI = tm.getDeviceId();
if (IMEI != null) {
tv=(TextView)findViewById(R.id.tv_imei_no);
tv.setText(IMEI);
}
detailDataBaseAdapter.insertEntry(telNumber,simID,IMEI);
}
});
bt=(Button)findViewById(R.id.getDetail);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String storedPassword=detailDataBaseAdapter.getData(telNumber);
Toast.makeText(MainActivity.this, storedPassword, Toast.LENGTH_LONG).show();
}
});
}
}
Logcat
FATAL EXCEPTION: main
Process: com.example.phone_detail, PID: 10548
E/AndroidRuntime(10548): java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.phone_detail.DetailDataBaseAdapter.getData(DetailDataBaseAdapter.java:59)
at com.example.phone_detail.MainActivity$2.onClick(MainActivity.java:59)
Upvotes: 2
Views: 1920
Reputation: 35
I believe that in the onCreate(...) method of your database helper, you should check first if the database exists in the required paths "/data/data/app-package/databases" and create it if it doesn't. Or else, you might want to consider adding IF NOT EXISTS to your create statements.
Upvotes: 0
Reputation: 737
logCat says that you are binding null
to query in your getData method - so perhaps you're calling this method with null
parameter. Try to check if parameter is null before calling db.query
Upvotes: 2