我已经经历了许多问题和答案,并发现更改数据库版本将被迫创建新表.... 我也尝试使用新名称创建表,但发生了同样的错误....
08-17 17:58:10.871 21817-21879/com.hp.registration E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa0da1e0 08-17 17:58:13.274 21817-21817/com.hp.registration E/SQLiteLog: (1) no such table: Regi
08-17 17:58:13.288 21817-21817/com.hp.registration E/SQLiteDatabase: Error inserting States=Hariyana PhoneNo=9874563219 Gender=Male Password=12345 Email=rehan@gmail.com Address=kalol Name=Rehan
我简单创建注册表并尝试将数据存储到数据库和我的JAVA注册活动(主要)文件代码是......
public class RegisterActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
EditText edtName, edtPhoneNo, edtemail, edtpassword, edtconformpassword, edtAddress;
RadioGroup radio;
boolean isvalid = true;
RadioButton radmale, radfemale;
Button btnregister,btnUpdate,btnDelete;
Spinner spinner;
TextView tv;
private static final String tablename = "Mydb1";
String strGender1;
Global_alert_box_main2 alert = new Global_alert_box_main2();
String item;
MyHelper mh;
SQLiteDatabase database;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getIds();
mh=new MyHelper(this);
database=mh.getWritableDatabase();
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.states, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
private void getIds() {
radio = (RadioGroup) findViewById(R.id.radio);
radmale = (RadioButton) findViewById(R.id.radmale);
radfemale = (RadioButton) findViewById(R.id.radfemale);
edtName = (EditText) findViewById(R.id.edtName);
edtPhoneNo = (EditText) findViewById(R.id.edtPhoneNo);
edtemail = (EditText) findViewById(R.id.edtemail);
edtpassword = (EditText) findViewById(R.id.edtpassword);
edtconformpassword =(EditText)findViewById(R.id.edtconformpassword);
edtAddress = (EditText) findViewById(R.id.edtAddress);
spinner = (Spinner) findViewById(R.id.spinner);
btnregister=(Button)findViewById(R.id.btnregister);
btnUpdate=(Button)findViewById(R.id.btnUpdate);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnregister.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnDelete.setOnClickListener(this);
}
public void onClick(View v) {
String states, gender, name, phoneno, email, address, password;
name = edtName.getText().toString();
phoneno = edtPhoneNo.getText().toString();
email = edtemail.getText().toString();
address = edtAddress.getText().toString();
password = edtpassword.getText().toString();
states = item;
switch (v.getId()) {
case R.id.btnregister:
validate();
int i = radio.getCheckedRadioButtonId();
RadioButton radiobtn = (RadioButton) findViewById(i);
strGender1 = radiobtn.getText().toString();
// int gendervalue=gender("male");
// Intent intent=new Intent(this,);
gender = radiobtn.getText().toString();
long l= mh.insertData(name, phoneno,gender,address,email,password,states,database);
if(l==-1)
{
Toast.makeText(this,"UnSuccessfull Inserted! ",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"Save Record Successfully!",Toast.LENGTH_LONG).show();
}
break;
case R.id.btnUpdate:
Intent updateIntent=new Intent(RegisterActivity.this,UpdateActivity.class);
startActivity(updateIntent);
break;
case R.id.btnDelete:
Intent del=new Intent(this,delete_activity.class);
startActivity(del);
break;
}
}
这是我的助手课程
public class MyHelper extends SQLiteOpenHelper {
private static final String DBname="students";
// private static final String TBname="registration";
private static final String tbname="Regi";
String str="CREATE TABLE IF NOT EXISTS "+tbname+" (id integer primary key autoincrement,NAME text,PHONENO text,GENDER text,ADDRESS text,EMAIL text,PASSWORD text,STATES text)";
SQLiteDatabase db;
MyHelper mh;
Context context;
private static final String id="id";
private static final String fname="FirstName";
private static final String lname="LastName";
public MyHelper(Context context) {
super(context, DBname, null, 1);
db=this.getReadableDatabase();
db=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(str);
Log.e("Table", "Table has been created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + tbname);
onCreate(db);
}
public long insertData(String NAME, String PHONENO,String GENDER,String ADDRESS,String EMAIL,String PASSWORD,String STATES, SQLiteDatabase db)
{
ContentValues contentValues=new ContentValues();
contentValues.put("Name",NAME);
contentValues.put("PhoneNo",PHONENO);
contentValues.put("Gender",GENDER);
contentValues.put("Address",ADDRESS);
contentValues.put("Email",EMAIL);
contentValues.put("Password",PASSWORD);
contentValues.put("States", STATES);
long status=db.insert(tbname, null, contentValues);
return status;
}
}
答案 0 :(得分:1)
更改您的代码
public long insertData(String NAME, String PHONENO,String GENDER,String ADDRESS,String EMAIL,String PASSWORD,String STATES, SQLiteDatabase db)
{
ContentValues contentValues=new ContentValues();
contentValues.put("Name",NAME);
contentValues.put("PhoneNo",PHONENO);
contentValues.put("Gender",GENDER);
contentValues.put("Address",ADDRESS);
contentValues.put("Email",EMAIL);
contentValues.put("Password",PASSWORD);
contentValues.put("States", STATES);
long status=db.insert(tbname, null, contentValues);
return status;
}
到
public long insertData(String NAME, String PHONENO,String GENDER,String ADDRESS,String EMAIL,String PASSWORD,String STATES)
{
db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("Name",NAME);
contentValues.put("PhoneNo",PHONENO);
contentValues.put("Gender",GENDER);
contentValues.put("Address",ADDRESS);
contentValues.put("Email",EMAIL);
contentValues.put("Password",PASSWORD);
contentValues.put("States", STATES);
long status=db.insert(tbname, null, contentValues);
return status;
}
运行此代码。我希望这能解决你的问题。一切顺利
答案 1 :(得分:1)
先生.Chirag真的,你帮我和其他所有试图解决我问题的人,我真的要感谢他们。
我发现我的android工作室有一些内部问题,因为其他设备上的相同代码工作非常顺利...建议"填充设备"由希拉克先生解决了我的问题。
答案 2 :(得分:0)
改变你的
String str="CREATE TABLE IF NOT EXISTS "+tbname+" (id integer primary key autoincrement,NAME text,PHONENO text,GENDER text,ADDRESS text,EMAIL text,PASSWORD text,STATES text)";
行到
String str= "CREATE TABLE " + tbname + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , NAME TEXT, PHONENO TEXT, GENDER TEXT, ADDRESS TEXT, EMAIL TEXT,PASSWORD TEXT,STATES TEXT)";
我希望它能为你的工作....................:)