SQLitedatabase實現訪問sqlite

來源:互聯網
上載者:User

標籤:des   android   http   io   ar   os   sp   java   strong   

通過SQLiteDatabase 來訪問sqlite資料庫

----Main.java

public class Main extends Activity {SQLiteDatabase db;ListView listView;EditText editText1, editText2;  //要添加的標題和contextButton button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);listView = (ListView) findViewById(R.id.listView1);editText1 = (EditText) findViewById(R.id.editText1);editText2 = (EditText) findViewById(R.id.editText2);button = (Button) findViewById(R.id.button1);// /data/data/com.example.dbtest/files --/my.db3  db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+ "/my.db3", null);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String str1 = editText1.getText().toString();String str2 = editText2.getText().toString();try {insertToDB(db, str1, str2);Cursor cursor = db.rawQuery("select * from news_info",null);inflateListView(cursor);} catch (SQLiteException e) {db.execSQL("create table news_info(_id integer primary key autoincrement,"+ "news_title varchar(50),"+ "news_content varchar(255))");insertToDB(db, str1, str2);Cursor cursor = db.rawQuery("selecte * from news_info",null);inflateListView(cursor);}}});}private void insertToDB(SQLiteDatabase db, String str1, String str2) {db.execSQL("insert into news_info values(null, ?, ?)", new String[] {str1, str2 });}private void inflateListView(Cursor cursor) {SimpleCursorAdapter adapter = new SimpleCursorAdapter(Main.this,R.layout.item, cursor, new String[] { "news_title","news_content" }, new int[] { R.id.textView1,R.id.textView2 },CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);listView.setAdapter(adapter);}@Overrideprotected void onDestroy() {super.onDestroy();if(db!=null&&db.isOpen()){db.close();}}}

SimpleCursorAdapter封裝Cursor時,要求資料表的主鍵列的列名為   _id  。因為SimpleCursorAdapter只能識別 列名為 

_id的主鍵。否則會報錯。

同java中的操作JDBC一樣,資料庫最後也要關閉  db.close(); 來回收資源。

---main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:ems="10" >        <requestFocus />    </EditText>    <EditText        android:id="@+id/editText2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/editText1"        android:ems="10" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/editText2"        android:text="插入資料" />    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/button1" >    </ListView></RelativeLayout>

listeview 每個子項的布局檔案 item.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="TextView" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginLeft="20dp"        android:layout_toRightOf="@+id/textView1"        android:text="TextView" /></RelativeLayout>

運行效果:


SQLitedatabase實現訪問sqlite

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.