我正在尝试在Android应用程序中创建和维护数据库"代码看起来很好,但点击提交按钮应用程序已经崩溃,这是不期望的;
layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.firstproject.saveinfoindatabase.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:hint="phone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="93dp"/>
</RelativeLayout>
mainActivity.java文件
public class MainActivity extends AppCompatActivity {
EditText name,phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText) findViewById(R.id.editText);
phone=(EditText) findViewById(R.id.editText2);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getNmae = name.getText().toString();
String getPhone = phone.getText().toString();
DataBaseOpreation objOFDataBaseOpreation = new DataBaseOpreation(getBaseContext());
objOFDataBaseOpreation.insertInfo(getNmae, getPhone);
Toast.makeText(getBaseContext(), "dataInserted", Toast.LENGTH_LONG).show();
objOFDataBaseOpreation.close();
}
});
}
}
DataBaseOpreation.java文件(此文件维护数据库)
public class DataBaseOpreation extends SQLiteOpenHelper{
public static final String DATA_BASE_NAME="mydatabase";
public static final String TABLE_NAME="save.info";
DataBaseOpreation(Context context){
super(context,DATA_BASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"create table save.info " +
"(id integer primary key, name text, phone text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS save.info");
onCreate(db);
}
public void insertInfo(String name,String phone){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("NAME",name);
contentValues.put("PHONE", phone);
db.insert(TABLE_NAME, null, contentValues);
}
public Cursor getData(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from save.info",null);
return res;
}
}
manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.firstproject.saveinfoindatabase"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
SQLiteOpenHelper是A helper class to manage database creation and version management. ,它通常与contentprovider一起使用来设置数据库表等。所以你有一半的解决方案。我建议您使用one of the code generation tools实现contentprovider,为您完成繁琐的工作。许多其他contententprovider生成器可用,如果它不适合您的需求。