使用SQL语句操做SQLite数据库

先上一张效果图: java

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

 

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

DBTest.java ide

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。 this

main.xml spa

<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进行数据库操做的步骤: code

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

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

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

4.关闭SQLiteDatabase,回收资源

相关文章
相关标签/搜索