SQLitedatabase实现访问sqlite
通过SQLiteDatabase 来访问sqlite数据库
----Main.java
public class Main extends Activity { SQLiteDatabase db; ListView listView; EditText editText1, editText2; //要添加的标题和context Button button; @Override protected 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() { @Override public 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); } @Override protected 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>
运行效果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。