求大神,android往sqlite中插入时出错
privateButtoncreate,updateData,insert,query,update;@OverridepublicvoidonCreate(Bundle...
private Button create, updateData, insert, query, update;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
create = (Button)findViewById(R.id.create);
update = (Button)findViewById(R.id.update);
updateData = (Button)findViewById(R.id.updateData);
insert = (Button)findViewById(R.id.insert);
query = (Button)findViewById(R.id.query);
create.setOnClickListener(new CreateListener());
update.setOnClickListener(new UpdateListener());
updateData.setOnClickListener(new UpdateDataListener());
insert.setOnClickListener(new InsertListener());
query.setOnClickListener(new QueryLisetner());
}
class CreateListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this, "myDB");
SQLiteDatabase db = dbHelper.getReadableDatabase();
}
}
问题提示:error opening trace file: No such file or directory (2)
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 展开
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
create = (Button)findViewById(R.id.create);
update = (Button)findViewById(R.id.update);
updateData = (Button)findViewById(R.id.updateData);
insert = (Button)findViewById(R.id.insert);
query = (Button)findViewById(R.id.query);
create.setOnClickListener(new CreateListener());
update.setOnClickListener(new UpdateListener());
updateData.setOnClickListener(new UpdateDataListener());
insert.setOnClickListener(new InsertListener());
query.setOnClickListener(new QueryLisetner());
}
class CreateListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this, "myDB");
SQLiteDatabase db = dbHelper.getReadableDatabase();
}
}
问题提示:error opening trace file: No such file or directory (2)
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 展开
3个回答
展开全部
创建数据库和版本 :
----------------------------------------------------------------------------------------------------------------------
public class BarCodeOpenHelper extends SQLiteOpenHelper{
public BarCodeOpenHelper(Context context) {
super(context, "barcode.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table barcode (_id integer auto_increment ,barcode varchar(100))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query("barcode", null,null, null, null, null, null);
return c;
}
}
----------------------------------------------------------------------------------------------------------------------
数据库DAO : (只有查询的操作)
----------------------------------------------------------------------------------------------------------------------
public class BarCodeDao {
private BarCodeOpenHelper helper;
public BarCodeDao(Context context) {
helper = new BarCodeOpenHelper(context);
}
public void add(String barcode) {
SQLiteDatabase db = helper.getWritableDatabase();
if (db.isOpen()) {
db.execSQL("insert into barcode (barcode) values (?)",new Object[] {barcode});
db.close();
}
}
注意 :建库的时候名字是xxx.db 不是xxx 。
----------------------------------------------------------------------------------------------------------------------
public class BarCodeOpenHelper extends SQLiteOpenHelper{
public BarCodeOpenHelper(Context context) {
super(context, "barcode.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table barcode (_id integer auto_increment ,barcode varchar(100))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query("barcode", null,null, null, null, null, null);
return c;
}
}
----------------------------------------------------------------------------------------------------------------------
数据库DAO : (只有查询的操作)
----------------------------------------------------------------------------------------------------------------------
public class BarCodeDao {
private BarCodeOpenHelper helper;
public BarCodeDao(Context context) {
helper = new BarCodeOpenHelper(context);
}
public void add(String barcode) {
SQLiteDatabase db = helper.getWritableDatabase();
if (db.isOpen()) {
db.execSQL("insert into barcode (barcode) values (?)",new Object[] {barcode});
db.close();
}
}
注意 :建库的时候名字是xxx.db 不是xxx 。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你要发一下错误,而不是简单的这几句代码啊!这几句代码能看不出什么问题。。。
追问
问题提示:error opening trace file: No such file or directory (2)
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
追答
你这三句错误,第一句是这个数据库没有建立成功啊。第二句是可能你前面测试填的数据库版本是2,后面测试改成了版本1?版本号只能往上递增的,否则你更新的时候不能把以前的版本给卸载,
第三句就自然是不能打开这个数据库咯?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SQLiteDatabase db = dbHelper.getReadableDatabase();这句话改为
SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteDatabase db = dbHelper.getWritableDatabase();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询