SQLite数据库(为什么这样做呢?) [英] SQLite Database (why do this?)

查看:70
本文介绍了SQLite数据库(为什么这样做呢?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是创造的android简单的SQLite数据库。

hi i am create a simple SQLite database in android.

我使用SQLite辅助类

i use the SQLite helper class

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

and i run this project then generate a error Logcat

9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):致命异常:主要
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.and.example.database / com.and.example.database.DatabaseDemoActivity}:android.database .sqlite.SQLiteException:表mytable10有没有名为纬度列:,在编译:插入mytable10(名称,经度,纬度,地址,contectno)值(,,,,?????)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.os.Handler.dispatchMessage(Handler.java:99)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.os.Looper.loop(Looper.java:123)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.main(ActivityThread.java:4627)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在java.lang.reflect.Method.invokeNative(本机方法)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在java.lang.reflect.Method.invoke(Method.java:521)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在dalvik.system.NativeStart.main(本机方法)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):android.database.sqlite.SQLiteException:产生的原因表mytable10有没有名为纬度列:,在编译:插入mytable10(名称,经度,纬度,地址,contectno)VALUES(?,?,?,?,?)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteCompiledSql.native_compile(本机方法)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteCompiledSql(SQLiteCompiledSql.java:64)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteProgram(SQLiteProgram.java:80)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteStatement(SQLiteStatement.java:36)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在com.and.example.helper.DatabaseHelper(DatabaseHelper.java:31)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在com.and.example.database.DatabaseDemoActivity.onCreate(DatabaseDemoActivity.java:23)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
9月3日至8日:16:57.963:ERROR / AndroidRuntime(1003):11 ...更多

03-08 09:16:57.963: ERROR/AndroidRuntime(1003): FATAL EXCEPTION: main 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.and.example.database/com.and.example.database.DatabaseDemoActivity}: android.database.sqlite.SQLiteException: table mytable10 has no column named latitude: , while compiling: insert into mytable10(name,latitude,longitude,address,contectno) values(?,?,?,?,?) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.os.Handler.dispatchMessage(Handler.java:99) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.os.Looper.loop(Looper.java:123) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at 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): Caused by: android.database.sqlite.SQLiteException: table mytable10 has no column named latitude: , while compiling: insert into mytable10(name,latitude,longitude,address,contectno) values(?,?,?,?,?) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:36) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at 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): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): ... 11 more

然后我解决误差

db.execSQL(CREATE TABLE+ TABLE_NAME +(ID INTEGER PRIMARY KEY,名字TEXT,REAL纬度,经度REAL,地址文本,contectno INTEGER));

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

和运行该项目,但在LogCat中产生同样的错误。

and run the project but generate the same error in LogCat.

然后我改变的唯一数据库名和表名等code是相同的,并成功运行的项目。

and then i change the only database name and table name other code is same and successful run project.

我的问题是
为什么这样呢?

my Question is Why do like this?

推荐答案

如果你不通过升级数据库版本删除现有的表或清除用户数据,该表将不会被没有这种'letitude创建错字在初始创建表了。

If you don't drop the existing tables by upgrading the database version or clear the user data, the table won't get created without that 'letitude' typo you had in the initial creation of the table.

清除用户数据,或删除该表中的onCreate()方法,它会得到了正确的列名重新创建。

Clear the user data, or drop the table in the onCreate() method, and it'll get re-created with the correct column name.

然后我改变的唯一数据库
  名和表名等code是相同的
  并成功运行的项目。

and then i change the only database name and table name other code is same and successful run project.

这是二/三如果更改数据库的名称,它会因为在用户的数据没有DB该名称经过onCreate()方法。

This is b/c if you change the db name, it will go through the onCreate() method since there is no db by that name in the users' data.

另外,请不要回来了跟进的问题,如果这些问题的答案不帮你。如果他们解决您的问题,请点击对号表明您同意。

Also, please post back with follow up questions if these answers don't help you. If they solve your issue, please click the checkmark to indicate your approval.

这篇关于SQLite数据库(为什么这样做呢?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆