之前我們使用的數據庫都是在代碼裡面創建的。下面介紹一下如果使用外部已有的sql數據庫。
先用SQLite管理工具,sqliteadmin 具體操作很簡單,在這裡我就不詳細介紹的了,但有一個地方時候很值得注意的,就是用sqliteadmin創建數據庫的時候,數據庫保存的路徑不能是中文路徑,中文路徑會出現下面的錯誤提示:
我在sqliteadmin 創建好數據庫StuDB,裡面的表如下:
650) this.width=650;">
將創建好的數據庫在DDMS中點擊650) this.width=650;">導入到data/data/程序的包名/
650) this.width=650;">
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>
效果圖:
650) this.width=650;" height=197>