重新启动活动时,SQLite数据库丢失了

时间:2015-05-13 10:20:53

标签: android

我创建了一个用于在数据库中插入数据并从数据库中检索数据的应用程序。当第一次活动开始然后数据插入数据库并从数据库中检索数据,但是当我重新启动活动并单击按钮从数据库中检索数据时,我的应用程序关闭时出错.Pleas帮助我??

DataBaseHelper类

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类

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类

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)

2 个答案:

答案 0 :(得分:2)

logCat表示你绑定null来查询你的getData方法 - 所以也许你用null参数调用这个方法。在调用db.query

之前,尝试检查参数是否为null

答案 1 :(得分:0)

我相信在你的数据库助手的onCreate(...)方法中,你应首先检查数据库是否存在于所需的路径“/ data / data / app-package / databases”中,如果它不存在则创建它“T。或者,您可能需要考虑将IF NOT EXISTS添加到您的create语句中。