我正在尝试制作登录页面,但此代码无法正常运行 应用程序运行tym说不幸已经停止了我不知道为什么。我是新的android任何一个plzz帮助。我很沮丧
数据库文件!!
public class RegistrationDetailsDataBaseFile extends SQLiteOpenHelper {
public static final String DATABASE_NAME="registrationDataBase.db";
public static final String TABLE_NAME="registrationTable";
public static final String NAME="name";
public static final String PASSWORD="password";
public static final String PHONE="phone";
public static final String ADDRESS="address";
private static final String QUERY="CREATE TABLE "+TABLE_NAME+"(id integer primary key autoincrement"+NAME+"not null"+PASSWORD+"not null"+PHONE+"not null"+ADDRESS+"not null)";
RegistrationDetailsDataBaseFile(Context context){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(QUERY);
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists"+TABLE_NAME);
onCreate(db);
}
public void setName(String nam){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(NAME,nam);
db.insert(TABLE_NAME,null,contentValues);
db.close();
}
}
从此文件发送数据
public class MainActivity extends AppCompatActivity {
EditText mEditTextName,mEditTextPassword,mEditTextPhone,mEditTextAddress;
Button mButtonRegistration;
String getName,getPassword,getPhone,getAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditTextName=(EditText)findViewById(R.id.name);
mEditTextPassword=(EditText)findViewById(R.id.password);
mEditTextPhone=(EditText)findViewById(R.id.phone);
mEditTextAddress=(EditText)findViewById(R.id.address);
mButtonRegistration=(Button)findViewById(R.id.register);
mButtonRegistration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getName=mEditTextName.getText().toString();
getPassword=mEditTextPassword.getText().toString();
getPhone=mEditTextPhone.getText().toString();
getAddress=mEditTextAddress.getText().toString();
RegistrationDetailsDataBaseFile objOfRegistrationDetailsDataBaseFile=new RegistrationDetailsDataBaseFile(getBaseContext());
objOfRegistrationDetailsDataBaseFile.setName(getName);
Toast.makeText(getBaseContext(), "name", Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:0)
您没有定义任何类型的列。还存在间距问题。要创建表,请按以下方式修改查询语句:
private static final String QUERY="CREATE TABLE "+TABLE_NAME+" (id integer primary key autoincrement, "+NAME+" TEXT NOT NULL, "+PASSWORD+" TEXT NOT NULL, "+PHONE+" TEXT NOT NULL, "+ADDRESS+"TEXT NOT NULL);";
还要修改onUpgrade()方法,如下所示:
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists "+TABLE_NAME);
onCreate(db);
}