之前我们使用的数据库都是在代码里面创建的。下面介绍一下如果使用外部已有的sql数据库。
先用SQLite管理工具,sqliteadmin 具体操作很简单,在这里我就不详细介绍的了,但有一个地方时候很值得注意的,就是用sqliteadmin创建数据库的时候,数据库保存的路径不能是中文路径,中文路径会出现下面的错误提示:
我在sqliteadmin 创建好数据库StuDB,里面的表如下:
将创建好的数据库在DDMS中点击导入到data/data/程序的包名/
SQLiteTestActivity.java
- package com.lingdududu.test;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class SQLiteTestActivity extends Activity {
- /** Called when the activity is first created. */
- private EditText studentText;
- private EditText teacherText;
- private Button queryBtn;
- SQLiteDatabase stuDb;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- studentText = (EditText)findViewById(R.id.stu_name);
- teacherText = (EditText)findViewById(R.id.teacher_name);
- queryBtn = (Button)findViewById(R.id.query);
- queryBtn.setOnClickListener(new queryListener());
- }
- class queryListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //调用查询方法
- query();
- stuDb.close();
- }
- }
- //查询方法
- private void query() {
- //打开或者创建数据库
- stuDb = SQLiteDatabase.openOrCreateDatabase("data/data/com.lingdududu.test/StuDB.s3db", null);
- try {
- String string =studentText.getText().toString();
- String sql = "Select sname from Student where snumber="+string;
- Cursor cursor = stuDb.rawQuery(sql,null);
- cursor.moveToFirst();
- teacherText.setText(cursor.getString(cursor.getColumnIndex("sname")));
- } catch (Exception e) {
- Toast.makeText(this, "请检查输入的学生学号是否正确", Toast.LENGTH_LONG).show();
- }
- }
- }
main.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"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/input_name"
- />
- <EditText
- android:id="@+id/stu_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/query"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="开始查询"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/teacher_name"
- />
- <EditText
- android:id="@+id/teacher_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:editable="false"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, SQLiteTestActivity!</string>
- <string name="app_name">SQLiteTest</string>
- <string name="input_name">请输入学生学号</string>
- <string name="teacher_name">该学生的姓名</string>
- </resources>
效果图:
本文转自 lingdududu 51CTO博客,原文链接:
http://blog.51cto.com/liangruijun/728272