我正在研究SQLite&当我尝试将数据插入我的应用程序然后我得到这个错误我创建表但它说没有数据库中的表

时间:2016-08-26 13:09:35

标签: android sqlite

当我尝试将数据插入我的应用程序时,我收到错误,说

  

没有这样的表:ReorderPages

我正在创建表student,但我仍然遇到此错误。

以下是我的代码:

主要活动

student

Dbhandler

public class MainActivity extends AppCompatActivity {
EditText editText_ID, editText_name, editText_city;
Button button_insert, button_all;
MyDBHandler mydb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText_ID = (EditText) findViewById(R.id.editText_ID);
    editText_name = (EditText) findViewById(R.id.editText_name);
    editText_city = (EditText) findViewById(R.id.editText_city);

    button_insert = (Button) findViewById(R.id.button_insert);
    button_all = (Button) findViewById(R.id.button_all);

    mydb = new MyDBHandler(this);
    //insert();
   // getAllData();

    button_insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean inserted = mydb.addStudent(new Student(editText_ID.getText().toString(), editText_name.getText().toString(), editText_city.getText().toString()));


            if (inserted = true)
                Toast.makeText(MainActivity.this, "Data is inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, "Data is Not inserted", Toast.LENGTH_LONG).show();

            mydb.close();
        }
    });

}

}

logcat的

public class MyDBHandler extends SQLiteOpenHelper {
public static final String DB_NAME="Student";
public static final String KEY_ID="key_id";
public static final String KEY_NAME="key_name";
public static final String KEY_CITY="key_city";
public static final String TBL_NAME="student";

public static final int DB_VERSION=4;
private final Context context;


public MyDBHandler(Context context) {
    super(context, DB_NAME, null,DB_VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {

 // String CREATE_TABLE="CREATE TABLE info(id INTEGER PRIMARY KEY,name TEXT,city TEXT)";
     db.execSQL("CREATE TABLE " + TBL_NAME + "(id INTEGER PRIMARY KEY,name TEXT,city TEXT)" );
//String CREATE_STUDENT_TABLE="CREATE TABLE " + TBL_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_CITY + " TEXT,"+")";

}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public boolean addStudent( Student student) {

    SQLiteDatabase db=getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID,student.getId());
    contentValues.put(KEY_NAME,student.getName());
    contentValues.put(KEY_CITY,student.getCity());

     int result= (int) db.insert(TBL_NAME,null,contentValues);

    if (result == -1)
    {
        return false;
    }
    else

        return true;
}

void searchStudent(int id){
    SQLiteDatabase db=getReadableDatabase();

    String search_query="SELECT * FROM TBL_NAME WHERE key_id="+id;
    try {
        Cursor cursor=db.rawQuery(search_query, null);
        if (cursor.getColumnCount()>0)
        {
            cursor.moveToFirst();

            Log.d("mytag",""+cursor.getString(0)+": "+cursor.getString(1)+" :"+cursor.getString(2));

        }
        else
        {
            Toast.makeText(context, "No records", Toast.LENGTH_SHORT).show();
        }


    }catch (Exception e){

        Log.d("mytag","MyDBHandler:SearchContact:  :"+e.getMessage() );
    }



}
}

我正在努力解决这个错误形式,但我不明白如何处理这个问题。

请建议我如何处理。

1 个答案:

答案 0 :(得分:0)

如果它告诉没有表,那么就没有表:)

但是,由于您在onCreate()注释掉了多个SQL stataments,我认为之前至少运行过一次旧代码。如果是这样,onCreate()不再被调用,因为它只在没有数据库时被调用 - 但是有旧数据库。您要检查的是,只需删除应用程序的旧数据即可。只需转到设备上的应用管理器并清除其所有数据 - 这也将删除数据库,然后在下次启动时调用onCreate()