我创建了一个简单的应用程序来获取学生的名字并将其保存在数据库中,如果插入,则显示吐司,否则显示“没有工作”吐司。如何找到我的数据库已保存?
这是我的脚本代码:
学生
public class Student {
public String Name;
}
的DatabaseManager
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseManager extends SQLiteOpenHelper {
public DatabaseManager(Context context)
{
super(context, "myData", null, 1);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
String query = "CREATE TABLE tbl_student_Info ( " +
" Name VARCHAR( 10 )," +
");";
arg0.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
String query = " DROP TABLE IF EXISTS tbl_student_Info ";
arg0.execSQL(query);
onCreate(arg0);
}
public boolean addStudent(Student student)
{
boolean result;
try
{
String query = "INSERT INTO tbl_student_Info (Name, Tel)" +
" VALUES ('" + student.Name + "')";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(query);
db.close();
result = true;
}
catch (Exception ex)
{
result = false;
}
return result;
}
MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TestDatabaseSimpleTestActivity extends Activity {
EditText edittext1;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle(" Registry page ");
edittext1 = (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String name = edittext1.getText().toString();
Student student = new Student();
student.Name = name;
DatabaseManager db = new DatabaseManager(getApplicationContext());
boolean result = db.addStudent(student);
if (result == true)
{
String s = "It works...";
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
else
{
String s = "Did not works...";
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
});
}
主要
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert" />
</LinearLayout>