DBHelper.java
package com.example.mysmallsql; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context, "person.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person(id integer primary key autoincrement,name varchar,number varchar)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
DBManager.java
package com.example.mysmallsql; import java.util.ArrayList; import java.util.List; import android.R.integer; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class DBManager { private DBHelper mdbHelper; private SQLiteDatabase db; public DBManager(Context context){ mdbHelper = new DBHelper(context); } public long add(String name,String number) { db = mdbHelper.getWritableDatabase(); //db.execSQL("insert into person (name,number) values(?,?)", new Object[]{name,number}); ContentValues values = new ContentValues(); values.put("name", name); values.put("number", number); long id = db.insert("person", null, values); db.close(); return id; } public boolean find(String name) { db = mdbHelper.getReadableDatabase(); //Cursor cursor = db.rawQuery("select * from person where name = ?", new String[]{name}); Cursor cursor = db.query("person", null, "name=?",new String[]{name} , null, null, null); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } //根据姓名修改号码 public int update(String name,String newnumber){ db = mdbHelper.getWritableDatabase(); //db.execSQL("update person set number = ? where name = ?", new Object[]{name,number}); ContentValues values = new ContentValues(); values.put("number",newnumber ); int number = db.update("person", values, "name=?", new String[]{newnumber}); db.close(); return number; } public int delete(String name) { db = mdbHelper.getWritableDatabase(); //db.execSQL("delete from person where name=?",new Object[]{name}); int number = db.delete("person", "name=?", new String[]{name}); db.close(); return number; } //查询全部 public List<Person> findall(){ db = mdbHelper.getReadableDatabase(); List<Person> persons = new ArrayList<Person>(); //Cursor cursor = db.rawQuery("select * from person", null); Cursor cursor = db.query("person", new String[]{"name","id","number"}, null, null, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); Person p = new Person(id, name, number); persons.add(p); } db.close(); return persons; } }
MainActivity.java
package com.example.mysmallsql; import android.os.Bundle; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Person.java
package com.example.mysmallsql; import android.R.integer; public class Person { private int id; private String name; private String number; public Person(){} public Person(int id, String name, String number) { this.id = id; this.name = name; this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }