主界面不需要改变,MainActivity:
package com.example.lianxi; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SQLiteDatabase db; db = openOrCreateDatabase("number.db", MODE_PRIVATE, null); String sql = "CREATE TABLE [students] (" +"[_id] INTEGER PRIMARY KEY AUTOINCREMENT," +"[_name] VARCHAR(50) UNIQUE NOT NULL," +"[_age] INT NOT NULL DEFAULT 16" +")"; db.execSQL(sql); } }
public SQLiteOpenHelper(Context context,String name,CursorFactory factory,int version)
public void onCreate(SQLiteDatabase db) public void onUpgrade(SQLiteDatabase db,int oldVersion ,int newVersion)
package com.example.lianxi; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper{ public DBOpenHelper(Context context){ super(context,"number2.db",null,1); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "CREATE TABLE [students] (" +"[_id] INTEGER PRIMARY KEY AUTOINCREMENT," +"[_name] VARCHAR(50) UNIQUE NOT NULL," +"[_age] INT NOT NULL DEFAULT 16" +")"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
DBOpenHelper helper = new DBOpenHelper(this);//使用这种方法可以反复执行 helper.getReadableDatabase();
public synchronized SQLiteDatabase getReadableDatabase() public synchronized SQLiteDatabase getWritableDatabase()
public void execsQL(String sql,Object[] bindArgs) throws SQLException
DBOpenHelper helper = new DBOpenHelper(this);//使用这种方法可以反复执行 SQLiteDatabase db = helper.getReadableDatabase(); String sql = "INSERT INTO students (name,age) VALUES(?,?)"; Object[] bindArgs = new Object[] {"Jaskdk",34}; db.execSQL(sql, bindArgs);
DBOpenHelper helper = new DBOpenHelper(this);//使用这种方法可以反复执行 SQLiteDatabase db = helper.getReadableDatabase(); String table = "students"; String nullColumnHack = null; ContentValues values = new ContentValues(); values.put("_name", "Mike"); values.put("_age", 28); long id = db.insert(table, nullColumnHack , values); if(id == -1){ Toast.makeText(this, "错误", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this, "插入记录成功", Toast.LENGTH_LONG).show(); }
MainActivity:
package com.example.lianxi; import android.app.Activity; import android.content.ContentValues; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private DBOpenHelper helper; private SQLiteDatabase db; private EditText name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); helper = new DBOpenHelper(this); db = helper.getReadableDatabase(); name = (EditText) findViewById(R.id.name); } public void find_All(View view){ Cursor cursor = db.query("students", new String[]{"_id","_name","_age"}, null, null, null, null, "_id desc"); long id; String name; int age; for(cursor.moveToFirst(); !cursor.isAfterLast();cursor.moveToNext()){ id = cursor.getLong(cursor.getColumnIndex("_id")); name = cursor.getString(cursor.getColumnIndex("_name")); age = cursor.getInt(cursor.getColumnIndex("_age")); System.out.println("id = " + id + " ," + "name = " +name +" ," + "age = " + age); } cursor.close(); } public void findName(View view){ String findName = name.getText().toString(); Cursor cursor = db.query("students", null, "_name=?", new String[]{findName}, null, null, null); if(cursor.moveToFirst()){ long id1; String name1; int age1; id1 = cursor.getLong(cursor.getColumnIndex("_id")); name1 = cursor.getString(cursor.getColumnIndex("_name")); age1 = cursor.getInt(cursor.getColumnIndex("_age")); Toast.makeText(this, "学生记录为id = " + id1 + " ," + "name = " +name1 + " ," + "age = " + age1, Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this, "没有匹配的记录!", Toast.LENGTH_LONG).show(); } } }
整体布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/find_all" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:onClick="find_All" android:text="查询所有数据" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="30dp" android:orientation="horizontal" > <EditText android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" > </EditText> <Button android:id="@+id/find" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:onClick="findName" android:text="查询" /> </LinearLayout> </LinearLayout>