SQLite数据库(为什么这样做?)

时间:2011-03-08 04:28:40

标签: android

嗨,我在android中创建一个简单的SQLite数据库。

我使用SQLite助手类

package com.and.example.helper;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DatabaseHelper {

    private static final String DATABASE_NAME = "mydata10.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "mytable10";

    private SQLiteDatabase db;
    private Context context;
    private SQLiteStatement insertStm;

    private static final String INSERT = "insert into " + TABLE_NAME + "(name,latitude,longitude,address,contectno) values(?,?,?,?,?)";


    public DatabaseHelper (Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase();
        //db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
        this.insertStm = this.db.compileStatement(INSERT);

    }

    private static class OpenHelper extends SQLiteOpenHelper {


        public void onCreate(SQLiteDatabase db) {

            db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT, letitude REAL, longitude REAL, address TEXT, contectno INTEGER)" );
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            Log.w("Example", "Upgrading database, this will drop table and recreate");
            db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
            onCreate(db);
        }

        public OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }
    }


    public long insert(String name, double latitude, double longitude, String address, int contactno){
        this.insertStm.bindString(1, name);
        this.insertStm.bindDouble(2, latitude);
        this.insertStm.bindDouble(3, longitude);
        this.insertStm.bindString(4, address);
        this.insertStm.bindLong(5, contactno);
        return this.insertStm.executeInsert();
    }

    public void deleteAll(){
        this.db.delete(TABLE_NAME, null, null);
    }

    public List<String>selectAll(){
        List<String>list = new ArrayList<String>();
        Cursor cursor = this.db.query(TABLE_NAME, new String[]{"name","latitude","longitude","address","contectno"}, null, null, null, null, null);

        if(cursor.moveToFirst())
        {
            do {
                list.add(cursor.getString(0));
                list.add(cursor.getString(1));
                list.add(cursor.getString(2));
                list.add(cursor.getString(3));
                list.add(cursor.getString(4));

            }while(cursor.moveToNext());

        }
        if(cursor != null && !cursor.isClosed())
        {
            cursor.close();
        }
        return list;
    }
}

我的主要活动课

package com.and.example.database;

import java.util.List;

import com.and.example.helper.DatabaseHelper;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class DatabaseDemoActivity extends Activity{

    private TextView textout;
    private DatabaseHelper sqlHelper;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textout = (TextView)findViewById(R.id.textview1);

        sqlHelper = new DatabaseHelper(this);

        sqlHelper.deleteAll();

        sqlHelper.insert("Durgesh",1.352566007,103.78921587,"ahmedabad",942974);
//        sqlHelper.insert("Mukesh");

        List<String>names = this.sqlHelper.selectAll();
        StringBuilder sb = new StringBuilder();
        sb.append("Name in Database: \n");
        for(String name : names)
        {
            sb.append(name + "\n");
        }

        Log.d("Example", "name size - " +names.size());
        this.textout.setText(sb.toString());
    }
}

然后运行此项目然后生成错误Logcat

03-08 09:16:57.963:ERROR / AndroidRuntime(1003):致命异常:主要 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):java.lang.RuntimeException:无法启动活动ComponentInfo {com.and.example.database / com.and.example.database.DatabaseDemoActivity}:android.database .sqlite.SQLiteException:table mytable10没有名为latitude的列:,同时编译:insert into mytable10(name,latitude,longitude,address,contectno)值(?,?,?,?,?) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.os.Handler.dispatchMessage(Handler.java:99) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.os.Looper.loop(Looper.java:123) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.main(ActivityThread.java:4627) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at java.lang.reflect.Method.invokeNative(Native Method) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at java.lang.reflect.Method.invoke(Method.java:521) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at dalvik.system.NativeStart.main(Native Method) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):引起:android.database.sqlite.SQLiteException:table mytable10没有名为latitude的列:,同时编译:insert into mytable10(name,latitude,longitude,address ,contectno)值(?,?,?,?,?) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteCompiledSql.native_compile(本机方法) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:64) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:80) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:36) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at com.and.example.helper.DatabaseHelper。(DatabaseHelper.java:31) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):at com.and.example.database.DatabaseDemoActivity.onCreate(DatabaseDemoActivity.java:23) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 03-08 09:16:57.963:ERROR / AndroidRuntime(1003):... 11更多

然后我解决了错误

db.execSQL(“CREATE TABLE”+ TABLE_NAME +“(id INTEGER PRIMARY KEY,名称TEXT,纬度REAL,经度REAL,地址TEXT,contectno INTEGER)”);

并运行项目但在LogCat中生成相同的错误。

然后我改变了唯一的数据库名称和表名,其他代码是相同的并且成功运行项目。

我的问题是 为什么这样?

1 个答案:

答案 0 :(得分:0)

如果您不通过升级数据库版本或清除用户数据来删除现有表,那么如果没有在表的初始创建中出现的'letitude'错误,则不会创建表。

清除用户数据,或将表放在onCreate()方法中,然后使用正确的列名重新创建。

  

然后我改变了唯一的数据库   名称和表名称其他代码相同   并成功运行项目。

这是b / c如果更改数据库名称,它将通过onCreate()方法,因为用户数据中没有该名称的数据库。

此外,如果这些答案对您没有帮助,请回复后续问题。如果他们解决了您的问题,请点击复选标记表示您的同意。