[英]SQLite android column not found
my code is correct is assume but even though it gives error coloumn not found for _hour & _min:假设我的代码是正确的,但即使它给出了 _hour 和 _min 未找到的错误栏:
package com.example.ifest;
public class ProfileView extends ListActivity{
int hour,min;
TimePicker tp ;
String e,e1;
static int p = 0;
Spinner spn ;
Button b1,b2,b3;
EditText et;
String str1;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("Create");
openDB();
p++;
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position == 0){
final Dialog build = new Dialog(ProfileView.this);
build.setTitle("String Name and Details");
build.setContentView(R.layout.activity_dialog);
build.show();
spn = (Spinner)build.findViewById(R.id.spinner1_Dialog);
et = (EditText) build.findViewById(R.id.editText1_Dialog);
b2 = (Button) build.findViewById(R.id.button1_Dialog);
tp = (TimePicker)build.findViewById(R.id.timePicker1_Dialog);
b3 = (Button)build.findViewById(R.id.button2_Dialog);
tp.setIs24HourView(true);
tp.setCurrentMinute(00);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
build.dismiss();
hour = tp.getCurrentHour();
min = tp.getCurrentMinute();
list.add(et.getText().toString());
addDB(et.getText().toString(),spn.getLastVisiblePosition(),hour,min);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
});
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
build.dismiss();
}
});
}
}
protected void addDB(String name,int id,int h,int m) {
DBHandler handle = new DBHandler(this);
SQLiteDatabase db = handle.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("_name",name);
if(id == 0)
cv.put("_type","WIFI");
else if(id == 1)
cv.put("_type","BLUETOOTH");
else if(id == 2)
cv.put("_type","MEDIA");
cv.put("_hour", h);
cv.put("_min", m);
db.insert("_table", null , cv);
db.close();
}
private void openDB() {
if(p != 0){
DBHandler handle = new DBHandler(this);
SQLiteDatabase db = handle.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM "+ "_table",null);
c.moveToFirst();
while(c.moveToNext()){
list.add(c.getString(1));
Log.d("cursor", c.getString(1));
}
c.close();
db.close();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflate = getMenuInflater();
inflate.inflate(R.menu.string_main,menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()){
case R.id.item1:
final Dialog build = new Dialog(ProfileView.this);
build.setTitle("String Name and Details");
build.setContentView(R.layout.activity_dialog);
build.show();
spn = (Spinner)build.findViewById(R.id.spinner1_Dialog);
et = (EditText) build.findViewById(R.id.editText1_Dialog);
b2 = (Button) build.findViewById(R.id.button1_Dialog);
tp = (TimePicker)build.findViewById(R.id.timePicker1_Dialog);
b3 = (Button)build.findViewById(R.id.button2_Dialog);
tp.setIs24HourView(true);
tp.setCurrentMinute(00);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
build.dismiss();
hour = tp.getCurrentHour();
min = tp.getCurrentMinute();
list.add(et.getText().toString());
addDB(et.getText().toString(),spn.getLastVisiblePosition(),hour,min);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
});
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
build.dismiss();
}
});
break;
case R.id.item2:
break;
case R.id.item3:
Intent i = new Intent("com.example.ifest.ABOUTUS");
startActivity(i);
break;
case R.id.item4:
finish();
break;
}
return true;
}
}
and the database class is:数据库类是:
public class DBHandler extends SQLiteOpenHelper{
private static final String DB_NAME = "event_db";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "_table";
private static final String EVENT_NAME = "_name";
private static final String EVENT_ID = "_no";
private static final String EVENT_TYPE = "_type";
private static final String EVENT_HOUR = "_hour";
private static final String EVENT_MINUTE = "_min";
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + EVENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ EVENT_NAME + " TEXT NOT NULL, " + EVENT_TYPE + " TEXT NOT NULL, " + EVENT_HOUR +" INTEGER, " + EVENT_MINUTE + " INTEGER " + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
is there anything wrong with the SQL exec statement means any space or something? SQL exec 语句有什么问题意味着任何空间或其他东西吗?
It looks like you have changed the table schema inside DBHandler#onCreate()
after running the app at least once.看起来您至少在运行应用程序一次后更改了
DBHandler#onCreate()
的表架构。 If so you need to increment DB_VERSION = 2
.如果是这样,您需要增加
DB_VERSION = 2
。
The database won't check the code inside onCreate()
for a new schema on it's own, you need to tell the database to look for changes after you have made them.数据库不会自行检查
onCreate()
的代码以获取新模式,您需要在进行更改后告诉数据库查找更改。 The easiest way to do this is by increasing the DB_VERSION
.最简单的方法是增加
DB_VERSION
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.