Android数据存储(二)

android提供了一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定数据库的SQLiteDatabase对象,接下来就可以通过SQLiteDatabase对象来管理、操作数据库了。

SQLiteDatabase的execSQL()方法可执行任意的SQL语句,包括带占位符的SQL语句。但由于该方法没有返回值,因此一般用于执行DDL语句与DML语句;如果需要执行查询语句,则可调用SQLiteDatabase的rawQuerry(String sql ,String[] selectionArgs)方法。例如:

//执行插入语句
db.execSQL("insert into news values(null,?,?)",new String[]{title,content});

界面布局文件,提供两个文本框,用户可以在这两个文本框中输入内容,当用户单击“插入”按钮时这两个文本框的内容将会被插入数据库。

<?xml version="1.0" encoding="UTF-8"?>

-<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/title"/>

<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/content" android:lines="2"/>

<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/ok" android:text="@string/insert"/>

<ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/show"/>

</LinearLayout>

下面是activity:

public class MainActivity 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(
				MainActivity.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();
		}
	}
}
 

SQLiteDatabase需要想jdbc编程中那样关闭statement和connection连接,否则会引发资源泄露,总结起来,使用SQLiteDatabase进行数据库连接操作步骤如下:

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

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

3.操作sql语句的执行结果,比如用SimpleCursorAdapter来封装Cursor

4.关闭SQLi特DATabase,回收资源。

在Android SDK的platform-tools目录下提供了一个sqlite3.exe文件,他是一个简单的SQLite数据库的管理工具,类似于MySQL提供的命令行窗口。有些时候,开发者可以利用其来查询,管理数据库。常用命令如下:

.database:查出当前数据库

.tables:查出当前数据库里的数据表

.help:查出sqlite3支持的命令


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android数据存储是一种将数据存储在设备上的技术,以便应用程序可以使用它们,无论它们是否在运行时。这使得应用程序可以使用一些简单且安全的存储方法,而无需直接与设备存储硬件进行交互。Android提供了几种不同的数据存储选项,包括: 1. **Shared Preferences**:这是最基本的数据存储方式,它允许应用程序存储和检索键值对数据。这对于小型数据存储需求非常有用,因为它们是本地存储并且对其他应用程序不可见。 2. **Files**:应用程序可以使用文件系统来存储和检索数据。这可以包括文本文件、图像、音频和视频文件等。文件系统存储数据可以在应用程序关闭后保持持久性。 3. **SQLite**:SQLite是一个轻量级的关系型数据库,它可以在设备上作为嵌入式数据库使用。SQLite提供了对数据的结构化查询支持,因此更适合存储大量数据。 4. **Content Providers**:Content Providers允许应用程序之间共享数据。它们使得跨应用程序访问和共享数据成为可能,这对于构建大型应用程序的跨应用程序功能非常有用。 5. **外部存储(如SD卡)**:Android允许应用程序访问设备的外部存储(如SD卡)。这可以用于存储大文件或大型数据集,但需要注意的是,访问外部存储可能需要额外的权限。 在选择适当的存储选项时,需要考虑数据的类型、大小、持久性和安全性。例如,如果数据只需要在应用程序运行时可用,Shared Preferences可能是一个合适的选择。然而,如果需要存储大量结构化数据或大型文件,那么SQLite或外部存储可能更适合。 此外,对于敏感数据(如用户密码或个人信息),建议使用加密来保护这些数据。在Android中,可以使用AES或其他加密算法来实现这一点。 最后,记住在处理存储数据时遵循最佳实践,例如使用适当的错误处理和日志记录,以确保数据的安全性和完整性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值