Android怎麼使用數據。
數據庫操作起來還比較簡單,但是緩存就比較難了。獨立項目的緩存技術就夠嗆的了。這裡先貼下數據庫的代碼吧!
創建數據庫的代碼:
package com.example.ex_templete;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteHelper extends SQLiteOpenHelper
{
public MySqliteHelper(Context context, int version)
{
super(context, "cache.db", null, version);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "CREATE TABLE person( " +
"persionid INTEGER PRIMARY KEY AUTOINCREMENT," +
"name VARCHAR(20), " +
"phone VARCHAR(20))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("ALTER TABLE person ADD salary");
}
}
使用數據庫的代碼:
package com.example.ex_templete;
import java.util.ArrayList;
import android.Manifest.permission;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener
{
private SQLiteDatabase mSqlDB;
private TextView mTextView;
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
insert();
break;
case R.id.button2:
delete();
break;
case R.id.button3:
update();
break;
case R.id.button4:
select();
break;
default:
break;
}
}
private void select()
{
String sql = "SELECT phone, name FROM person WHERE name=? ORDER BY name DESC";
Cursor cursor = mSqlDB.rawQuery(sql, new String[]{"Android"});
// Cursor cursor = mSqlDB.query("person", new String[]{"phone", "name"}, "name=?", new String[]{"Android"}, null, null, null);
boolean hasData = cursor.moveToFirst();
ArrayList<Person> mData = new ArrayList<MainActivity.Person>();
while (hasData)
{
Person person = new Person();
person.name = cursor.getString(cursor.getColumnIndex("name"));
person.phone = cursor.getString(cursor.getColumnIndex("phone"));
mData.add(person);
hasData = cursor.moveToNext();
}
mTextView.setText(mData.toString());
}
class Person
{
String name;
String phone;
@Override
public String toString()
{
return "Person [name=" + name + ", phone=" + phone + "]";
}
}
private void update()
{
// String sql = "UPDATE person SET phone=? WHERE name='Android'";
// mSqlDB.execSQL(sql , new String[]{"133333333"});
ContentValues values = new ContentValues();
values.put("phone", "133333333");
mSqlDB.update("person", values, "name=?", new String[]{"Android"});
}
private void delete()
{
// String sql = "DELETE FROM person WHERE name='Android'";
// mSqlDB.execSQL(sql );
mSqlDB.delete("person", "name=?", new String[]{"Android"});
}
private void insert()
{
// String sql = "INSERT INTO person (name, phone) VALUES" +
// " ('Android', '13888888888')";
// mSqlDB.execSQL(sql);
ContentValues values = new ContentValues();
values.put("name", "Android");
values.put("phone", "13888889999");
mSqlDB.insert("person", null, values);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
mTextView = (TextView) findViewById(R.id.textView1);
MySqliteHelper mSqliteHelper = new MySqliteHelper(this, 1);
mSqlDB = mSqliteHelper.getWritableDatabase();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11