使用SQL语句操作SQLite数据库

先上一张效果图:

每次点击“插入”,都会在下面的ListView中新增一行

 

本文涉及四个文件,分别是DBTest.java、main.xml、line.xml、strings.ml

DBTest.java

public class DBTest extends Activity {

	SQLiteDatabase db;
	Button btn = null;
	ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//获取路径,也算是一种调试的方法吧
		Log.d("myLog", this.getFilesDir().toString());
//创建或打开数据库(此处需要使用据对路径)
		db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir()
				.toString() + "/my.db3", null);
		listView = (ListView) findViewById(R.id.show);
		btn = (Button) findViewById(R.id.ok);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View source) {
				//获取用户输入
				String title = ((EditText)findViewById(R.id.title))
						.getText().toString();
				String content = ((EditText)findViewById(R.id.content))
						.getText().toString();
//如果没有数据库表就执行catch语句,先创建表,再执行其他语句
				try{
					insertData(db, title, content);
					Cursor cursor = db.rawQuery("select * from news_inf", null);
					inflateList(cursor);
				}catch(SQLiteException se){
					//执行DDL创建数据表
					db.execSQL("create table news_inf(_id integer primary key autoincrement,"
							+" news_title varchar(50),"
							+" news_content varchar(255))");
					//执行insert语句插入数据
					insertData(db, title, content);
					//执行查询
					Cursor cursor = db.rawQuery("select * from news_inf", null);
					inflateList(cursor);
				}
			}
		});
	}

	
	private void insertData(SQLiteDatabase db
			, String title , String content){
		//执行插入语句
		db.execSQL("insert into news_inf values(null , ? , ?)"
				, new String[]{title , content});
	}
	
	private void inflateList(Cursor cursor){
			//填充SimpleCursorAdapter
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				DBTest.this, R.layout.line, cursor
				, new String[]{"news_title", "news_content"}
				, new int[]{R.id.my_title, R.id.my_content});
		//显示数据
		listView.setAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//退出程序时关闭SQLiteDatabase
		if(db != null && db.isOpen()){
			db.close();
		}
	}
	
	

}

其中db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null);用于创建或打开SQLite数据库。当点击按钮的时候,程序会调用insertData方法,向底层数据库表中插入一行记录。然后再执行查询语句,把底层数据表中的记录查询出来,并使用ListView将查询结果(Cursor)显示出来。

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				DBTest.this, R.layout.line, cursor
				, new String[]{"news_title", "news_content"}
				, new int[]{R.id.my_title, R.id.my_content});

以上代码用于将Cursor封装成SimpleCursorAdapter,这个SimpleCursorAdapter实现了Adapter接口,可以作为ListView的内容适配器。Cursor里的每一行可以当成Map处理(以数据列的列名为key,数据列的值为value)。SimpleCursorAdapter这里有5个参数DBTest.this就是当前文件,R.layout.line是line.xml布局文件,cursor是执行完SQl语句之后,获取的游标,第4个参数是from,第5个参数是to,可以理解为将数据库里的news_title、news_content的字段值赋给line.xml里面的my_title、my_content。

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".DBTest" >

    <EditText 
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <EditText 
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:lines="2"/>
    <Button 
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/insert"
        />
	<ListView 
	    android:id="@+id/show"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    />
</LinearLayout>

 

line.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<EditText 
	android:id="@+id/my_title"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:width="120px"
	/>
<EditText  
	android:id="@+id/my_content"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	/>	
</LinearLayout>

 

strings.xml中加入下面一行 ,一般按钮、TextView的文字都写在这个文件中,这样方便维护。比如说需要修改某些字,或者提供不同的语言版本的时候,可以直接在里面改。当然有些语言开发者不熟悉的时候,可以直接把整个文件给有能力翻译的人,这样非常方便。

<string name="insert">插入</string>

 

总结使用SQLiteDatabase进行数据库操作的步骤:

1.获取SQLiteDatabase对象,它代表了与数据库的连接

2.调用SQLiteDatabase的方法来执行SQL语句

3.操作SQL语句的执行效果,比如用SimpleCursorAdapter封装Cursor

4.关闭SQLiteDatabase,回收资源

 

 

转载于:https://my.oschina.net/zjchase/blog/103417

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值