編輯:關於Android編程
* @author Administrator yangchao
*為了實現對數據庫版本進行管理,SQLiteOpenHelper類提供了兩個重要的方法,
*分別是onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
*/
public class DBOpenHelper extends SQLiteOpenHelper
{
private static final String DatabaseName = "test.db";//數據庫的名稱
private static final int DatabaseVersion = 1;//數據庫的版本號
public DBOpenHelper(Context context)
{
super(context, DatabaseName, null, DatabaseVersion);
}
/*
* 用於初次使用軟件時生成數據庫表
*/
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE person (personId integer primary key autoincrement, name varchar(20))");
}
/*
* 用於升級軟件時更新數據庫表結構。
* 只有在數據庫版本發生改變時 才會被調用
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
------------------------------------------------------------------------------------------------------------------------------------
public class PersonService
{
DBOpenHelper dbOpenHelper;
public PersonService(){}
public PersonService(Context context)
{
this.dbOpenHelper = new DBOpenHelper(context);
}
/**
* 保存方法
* @param person
*/
public void save(Person person)
{
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
String sql = "insert into person(personId, name) values(?, ?)";
database.execSQL(sql, new Object[]{person.getPersonId(), person.getName()});
}
/**
* 更改方法
* @param person
*/
public void update(Person person)
{
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
String sql = "update person set name=? where personId=?";
database.execSQL(sql, new Object[]{person.getName(), person.getPersonId()});
}
/**
* 刪除方法
* @param person
*/
public void delete(Person person)
{
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
String sql = "delete from person where personId=?";
database.execSQL(sql, new Object[]{person.getPersonId()});
}
/**
* 查找單個記錄
* @param id
* @return
*/
public Person find(Integer id)
{
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
String sql = "select * from person where personId = ?";
Cursor cursor = database.rawQuery(sql, new String[]{id.toString()});
if(cursor.moveToFirst())
{
String name = cursor.getString(cursor.getColumnIndex("name"));
int personId = cursor.getInt(cursor.getColumnIndex("personId"));
return new Person(personId, name);
}
return null;
}
/**
* 分頁查找
* @param offset
* @param maxResult
* @return
*/
public List
{
List
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
String sql = "select * from person limit ?, ?";
Cursor cursor = database.rawQuery(sql, new String[]{offset.toString(), maxResult.toString()});
while(cursor.moveToNext())
{
int personId = cursor.getInt(cursor.getColumnIndex("personId"));
String name = cursor.getString(cursor.getColumnIndex("name"));
persons.add(new Person(personId, name) );
}
return persons;
}
/**
* 獲取總記錄數
* @return
*/
public long getCount()
{
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
String sql = "select count(*) from person";
Cursor cursor = database.rawQuery(sql, null);
cursor.moveToFirst();
return cursor.getLong(0);
}
}
====================================測試======================================
public class DBTest extends AndroidTestCase
{
private static final String TAG = "DBTest";
public void testCreateDB()
{
DBOpenHelper helper = new DBOpenHelper(this.getContext());
/*
* 當調用SQLiteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法獲取
* 用於操作數據庫的SQLiteDatabase實例的時候,如果數據庫不存在,Android系統會自動生成一個數據庫,
* 接著調用onCreate()方法
*/
helper.getWritableDatabase();
}
public void testSava() throws Exception
{
PersonService service = new PersonService(this.getContext());
service.save(new Person(1, "楊超"));
service.save(new Person(2, "楊超1"));
service.save(new Person(3, "楊超2"));
}
public void testDelete()
{
}
public void testFind()
{
PersonService service = new PersonService(this.getContext());
Person person = service.find(4);
Log.i(TAG, person.toString());
}
public void testFenye()
{
}
public void testCount()
{
}
}
**前言**在Android中,我們一般通過RadioGroup來管理一組RadioButton 來達到 單選按鈕的互斥效果。但是,有些需求中,需要完成N行N列這樣的Ra
前言:1.文中的”$”不納入命令行中,只做標記用2.文中會介紹常用的命令行操作方式和xcode界面化操作方式介紹git3.以下均個人理解整理,如果
一、Activity 生命周期 二、Fragment 生命周期 三、對比圖 四、測試代碼 [java] pa
此前我們用HorizontalScrollView也實現了類似網易選項卡動態滑動效果,詳見 Android選項卡動態滑動效果這篇文章這裡我們用TabLayout來實現這一