使用System.out显示结果
main.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/createButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="创建数据库"/> <Button android:id="@+id/createTableButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="创建表"/> <Button android:id="@+id/insertButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="插入一条数据" /> <Button android:id="@+id/deleteButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="删除一条数据(依据姓名)" /> <Button android:id="@+id/updateButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="修改年龄" /> <Button android:id="@+id/queryAllButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="查询所有数据" /> <Button android:id="@+id/queryWithConditionButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="查询某一数据" /> <Button android:id="@+id/dropButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="删除表" /> <Button android:id="@+id/closeButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="关闭数据库" /> <EditText android:id="@+id/nameEditText" android:layout_marginTop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="输入姓名"/> <EditText android:id="@+id/ageEditText" android:layout_marginTop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:numeric="integer" android:hint="输入年龄"/> <EditText android:id="@+id/proEditText" android:layout_marginTop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="输入职业"/> </LinearLayout> </ScrollView>
SQLiteExampleActivity.java
package com.zeph.android.sqlite.example; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; 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 SQLiteExampleActivity extends Activity { private Button createButton; private Button createTableButton; private Button insertButton; private Button deleteButton; private Button updateButton; private Button queryAllButton; private Button queryWithConditionButton; private Button dropButton; private Button closeButton; private EditText nameEditText; private EditText ageEditText; private SQLiteDatabase mySqLiteDatabase; private static final String DB_CREATE = "CREATE TABLE Persons(Id_P int PRIMARY KEY,Name varchar(255) NOT NULL,Age int NOT NULL,Professional varchar(255))"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createButton = (Button) findViewById(R.id.createButton); createTableButton = (Button) findViewById(R.id.createTableButton); insertButton = (Button) findViewById(R.id.insertButton); deleteButton = (Button) findViewById(R.id.deleteButton); updateButton = (Button) findViewById(R.id.updateButton); queryAllButton = (Button) findViewById(R.id.queryAllButton); queryWithConditionButton = (Button) findViewById(R.id.queryWithConditionButton); dropButton = (Button) findViewById(R.id.dropButton); closeButton = (Button) findViewById(R.id.closeButton); nameEditText = (EditText) findViewById(R.id.nameEditText); ageEditText = (EditText) findViewById(R.id.ageEditText); createButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SQLDataBaseHelper helper = new SQLDataBaseHelper( getApplicationContext(), "myDataBase", 1); // 创建或打开myDataBase,其中的SQLDataBaseHelper类的onCreate方法只会在第一次创建时使用,也就是说,创建数据库时,会同时创建一张表 mySqLiteDatabase = helper.getWritableDatabase(); Toast.makeText(getApplicationContext(), "创建数据库", Toast.LENGTH_SHORT).show(); } }); createTableButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mySqLiteDatabase.execSQL(DB_CREATE);// 创建表 } }); insertButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { if (!nameEditText.getText().toString().equals("") && !ageEditText.getText().toString().equals("")) { ContentValues values = new ContentValues(); int age = Integer.parseInt(ageEditText.getText() .toString()); values.put("Name", nameEditText.getText().toString()); values.put("Age", age); mySqLiteDatabase.insert("Persons", null, values); } else { Toast.makeText(getApplicationContext(), "姓名或年龄为空", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "请首先创建或打开数据库", Toast.LENGTH_SHORT).show(); } } }); deleteButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { if (!nameEditText.getText().toString().equals("")) { mySqLiteDatabase .execSQL("DELETE FROM Persons WHERE Name='" + nameEditText.getText().toString() + "'"); Toast.makeText(getApplicationContext(), "删除", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "姓名不能为空", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "请首先创建或打开数据库", Toast.LENGTH_SHORT).show(); } } }); updateButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { if (!nameEditText.getText().toString().equals("") && !ageEditText.getText().toString().equals("")) { ContentValues values = new ContentValues(); values.put("Age", Integer.parseInt(ageEditText .getText().toString())); mySqLiteDatabase .update("Persons", values, "Name='" + nameEditText.getText().toString() + "'", null); Toast.makeText(getApplicationContext(), "修改", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "姓名和年龄不能为空不能为空", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "请首先创建或打开数据库", Toast.LENGTH_SHORT).show(); } } }); queryWithConditionButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { if (!nameEditText.getText().toString().equals("")) { Cursor cursor = mySqLiteDatabase.rawQuery( "SELECT * FROM Persons WHERE Name='" + nameEditText.getText().toString() + "'", null); if (cursor != null) { if (cursor.moveToFirst()) { do { System.out.println(cursor.getString(1) + " " + cursor.getInt(2)); } while (cursor.moveToNext()); } } } else { Toast.makeText(getApplicationContext(), "姓名不能为空", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "请首先创建或打开数据库", Toast.LENGTH_SHORT).show(); } } }); queryAllButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { Cursor cursor = mySqLiteDatabase.rawQuery( "SELECT * FROM Persons", null); if (cursor != null) { if (cursor.moveToFirst()) { do { System.out.println(cursor.getString(1) + " " + cursor.getInt(2)); } while (cursor.moveToNext()); } } } else { Toast.makeText(getApplicationContext(), "请首先创建或打开数据库", Toast.LENGTH_SHORT).show(); } } }); dropButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { mySqLiteDatabase.execSQL("DROP TABLE IF EXISTS Persons"); Toast.makeText(getApplicationContext(), "删除表", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "请首先创建或打开数据库", Toast.LENGTH_SHORT).show(); } } }); closeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mySqLiteDatabase != null) { mySqLiteDatabase.close(); Toast.makeText(getApplicationContext(), "关闭数据库", Toast.LENGTH_SHORT).show(); } } }); } /** * 当需要创建或打开一个数据库并获得数据库对象时,首先根据指定的文件名创建一个辅助对象, 然后调用该对象的getWritableDatabase 或 * getReadableDatabase方法 获得SQLiteDatabase 对象。调用getReadableDatabase * 方法返回的并不总是只读数据库对象,一般来说该方法和getWriteableDatabase * 方法的返回情况相同,只有在数据库仅开放只读权限或磁盘已满时才会返回一个只读的数据库对象。 * * @author Ben Zeph * */ private class SQLDataBaseHelper extends SQLiteOpenHelper { private static final String DB_CREATE = "CREATE TABLE Persons(Id_P int PRIMARY KEY,Name varchar(255) NOT NULL,Age int NOT NULL,Professional varchar(255))"; /** * 得到一个SQLiteOpenHelper的对象, * 调用该对象的getWritableDatabase和getReadableDatabase方法 * ,可以得到所创建SQLiteDatabase。 * * @param context * @param name * @param factory * @param version */ public SQLDataBaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /** * * @param context * @param name * @param version */ public SQLDataBaseHelper(Context context, String name, int version) { super(context, name, null, version); } /** * 当数据库被首次创建时执行该方法,一般将创建表等初始化操作在该方法中执行。 */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DB_CREATE);// 创建数据库 } /** * 当打开数据库时传入的版本号与当前的版本号不同时会调用该方法。 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS notes"); onCreate(db); } } }