您的位置:首页 > 数据库

使用SQLiteDatabase操作SQLite数据库存储数据

2015-04-07 17:34 190 查看
MainActivity.java
package practise.lxm.hello;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Environment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity {

SQLiteDatabase db;
EditText edTxtTitle;
EditText edTxtContent;
ListView lvNews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edTxtTitle = (EditText)findViewById(R.id.edTxt_title);
edTxtContent = (EditText)findViewById(R.id.edTxt_content);
lvNews = (ListView)findViewById(R.id.lv_test);
Button btn = (Button)findViewById(R.id.btn_commit);

//打开或创建数据库
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
try {
File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "//test.db3");
db = SQLiteDatabase.openOrCreateDatabase(file,null);

} catch (IOException e) {
e.printStackTrace();
}
}

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
insert(db, edTxtTitle.getText().toString(), edTxtContent.getText().toString()); //插入
}catch (SQLiteException sql){
//失败创建表
String strSql = "create table news(_id integer primary key autoincrement, title nvarchar(50) not null, content nvarchar(100) not null)";
db.execSQL(strSql);
//重新插入
insert(db, edTxtTitle.getText().toString(), edTxtContent.getText().toString());
}
//刷新列表
String strSql = "select * from news";
Cursor cursor= db.rawQuery(strSql, null); //查询
inflateList(cursor);
}
});
}

//插入信息
private void insert(SQLiteDatabase db,String title, String content){
String strInsert = "insert into news(title,content) values(?,?)";
db.execSQL(strInsert,new String[]{title,content});
}

//显示数据到列表
private void inflateList(Cursor cursor){
//表中必须含有名为"_id"的列,该列数据类型等无所谓
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.lv_file_item,cursor,
new String[]{"title","content"},new int[]{R.id.tv_title,R.id.tv_content},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lvNews.setAdapter(simpleCursorAdapter);
}

protected void onDestroy() {
if(db != null) { //关闭数据库连接
db.close();
}
super.onDestroy();
}

}

lv_file_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/tv_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="@+id/tv_content"/>
</LinearLayout>


activity_main.xml

<TableLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:padding="5pt">
<TableRow android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edTxt_title"/>
</TableRow>
<TableRow android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edTxt_content"/>
</TableRow>
<Button
android:text="提交"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_commit"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_test"></ListView>
</TableLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: