android SQLite

package org.crazyit.db;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class DBTest extends Activity
{
	SQLiteDatabase db;
	Button bn = null;
	ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 创建或打开数据库(此处需要使用绝对路径)
		db = SQLiteDatabase.openOrCreateDatabase(
			this.getFilesDir().toString()
			+ "/my.db3", null); // ①
		listView = (ListView) findViewById(R.id.show);
		bn = (Button) findViewById(R.id.ok);
		bn.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();
				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 },
			CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); //③
		// 显示数据
		listView.setAdapter(adapter);
	}

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


xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
	
<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>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值