我创建了一个名为SQliteAdapter的类,这里是代码
package com.project.ElectronicBloodBank;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQliteAdapter {
SQliteHelper helper;
public SQliteAdapter(Context context) {
// TODO Auto-generated constructor stub
helper=new SQliteHelper(context);
}
public long inertData(String name, String age, String address, String phone, String bloodtype)
{
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(SQliteHelper.NAME, name);
contentValues.put(SQliteHelper.AGE, age);
contentValues.put(SQliteHelper.ADDRESS, address);
contentValues.put(SQliteHelper.PHONE, phone);
contentValues.put(SQliteHelper.BLOODTYPE, bloodtype);
long id=db.insert(SQliteHelper.NAME, null, contentValues);
return id;
}
static class SQliteHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME="Ebbdb";
private static final String TABLE_NAME="Donor";
private static final int DATABASE_VERSION=1;
private static final String UID="_id";
private static final String NAME="Name";
private static final String AGE="Age";
private static final String ADDRESS="Address";
private static final String PHONE="Phone";
private static final String BLOODTYPE="Bloodtype";
private static final String CREAT_TABLE="CREATE TABLE"+TABLE_NAME+"("+UID+" integer PRIMARY KEY autoincrement, "+NAME+" VARCHAR(200), "+AGE+" TEXT, "+ADDRESS+" TEXT, "+PHONE+" TEXT, "+BLOODTYPE+" TEXT);";
private static final String DROP_TABLE="DROP TABLE IF EXISTS"+TABLE_NAME;
private Context context;
public SQliteHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
Message.message(context, "constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREAT_TABLE);
Message.message(context, "onCreate called");
} catch (SQLException e) {
// TODO Auto-generated catch block
Message.message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
Message.message(context, "onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
Message.message(context, ""+e);
}
}
}
}
我还创建了一个将此代码插入我的ActivityTwo
package com.project.ElectronicBloodBank;
import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class ActivityTwo extends ActionBarActivity {
EditText etname, etage, etaddress, etphone, etbloodtype;
SQliteAdapter SQliteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
etname=(EditText) findViewById(R.id.etname);
etage=(EditText) findViewById(R.id.etage);
etaddress=(EditText) findViewById(R.id.etaddress);
etphone=(EditText) findViewById(R.id.etphone);
etbloodtype=(EditText) findViewById(R.id.etbloodtype);
SQliteHelper=new SQliteAdapter(this);
}
public void btnsave(View view)
{
String nam=etname.getText().toString();
String age=etage.getText().toString();
String addrs=etaddress.getText().toString();
String phn=etphone.getText().toString();
String btyp=etbloodtype.getText().toString();
long id=SQliteHelper.inertData(nam, age, addrs, phn, btyp);
if(id<0)
{
Message.message(this, "Unsuccessful");
}
else
{
Message.message(this, "Successfuly Inserted Donor");
}
}
}
问题是: 数据库已创建,但没有表! 我在logcat中填写它显示的信息
11-05 01:00:55.927: I/SqliteDatabaseCpp(650): sqlite returned: error
code = 1, msg = no such table: Name,
db=/data/data/com.project.ElectronicBloodBank/databases/Ebbdb
and bellow
11-05 01:00:55.947: E/SQLiteDatabase(650): Error inserting Name=omar
Bloodtype=+AB Phone=733310906 Age=23 Address=Sana'a
那么主要问题在哪里呢?
答案 0 :(得分:2)
首先,尝试更改此行:
long id=db.insert(SQliteHelper.NAME, null, contentValues);
使用:
long id=db.insert(SQliteHelper.TABLE_NAME, null, contentValues);
答案 1 :(得分:0)
我看到了几个问题,虽然主要问题是由jucas指出的问题。
对于您正在使用的everyupdate(onUpgrade调用),您始终会删除整个数据库并创建新数据库。这可能很危险。我建议你在这里创建一些保护并跟踪数据库版本。
我可以看到你将SQliteAdapter的实例称为SQliteHelper。我觉得这很危险。尝试创建一个不是其他类型的简单名称,例如mSqadapter。如果这可能是错误的来源,我不是那么专业,但对我来说听起来很奇怪。
应用jucas的建议(由SQliteHelper.TABLE_NAME替换SQliteHelper.NAME)会给您带来不同的错误。你能在报告中显示新的错误吗?这可能有助于查找错误。
无论如何,我建议也尝试我所包含的第二个建议。
希望它有所帮助。