这样的表与数据库Android

时间:2015-08-12 16:21:33

标签: android

我的db有这个问题......

08-12 18:09:35.165    5898-5898/com.example.giuseppe.appandroid E/SQLiteLog﹕ (1) no such table: INTER
08-12 18:09:35.166    5898-5898/com.example.giuseppe.appandroid D/AndroidRuntime﹕ Shutting down VM
08-12 18:09:35.168    5898-5898/com.example.giuseppe.appandroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.giuseppe.appandroid, PID: 5898
    android.database.sqlite.SQLiteException: no such table: INTER (code 1): , while compiling: INSERT INTO INTER VALUES (?,?,?,?,?,?,?,?,?,?);
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

这是我的数据库的代码......

package com.example.giuseppe.appandroid.helper;

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

import com.example.giuseppe.appandroid.app.Inter;

import java.util.ArrayList;
import java.util.Date;


public class DBInter {
private InterHelper mHelper;
private SQLiteDatabase mDatabase;

public DBInter(Context context) {
mHelper = new InterHelper(context);
mDatabase = mHelper.getWritableDatabase();
}
public void insertAllInter(ArrayList<Inter> listInter) {
String sql = "INSERT INTO "+ InterHelper.TABLE_INTER +"VALUES   
(?,?,?,?,?,?,?,?,?,?);";
SQLiteStatement statement = mDatabase.compileStatement(sql);
    mDatabase.beginTransaction();
    for (int i = 0; i < listInter.size(); i++) {
        Inter currentInter = listInter.get(i);
        statement.clearBindings();
        statement.bindString(2, currentInter.getAttr1());
        statement.bindString(3, currentInter.getAttr2());
        statement.bindString(4, currentInter.getAttr3());
        statement.bindString(5, currentInter.getAttr4());
        statement.bindString(6, currentInter.getAttr5());
        statement.bindString(7, currentInter.getAttr6());
        statement.bindString(8, currentInter.getAttr7());
        statement.bindString(9, currentInter.getAttr8());
        statement.bindString(10, currentInter.getAttr9());
         statement.execute();
    }
    mDatabase.setTransactionSuccessful();
    mDatabase.endTransaction();
}

public void insertInter(Inter inter) {

    String sql = "INSERT INTO " + InterHelper.TABLE_INTER + " VALUES    (?,?,?,?,?,?,?,?,?,?);";

    SQLiteStatement statement = mDatabase.compileStatement(sql);
    mDatabase.beginTransaction();
    statement.clearBindings();
    statement.bindString(2, inter.getAttr1());
    statement.bindString(3, inter.getAttr2());
    statement.bindString(4, inter.getAttr3());
    statement.bindString(5, inter.getAttr4());
    statement.bindString(6, inter.getAttr5());
    statement.bindString(7, inter.getAttr6());
    statement.bindString(8, inter.getAttr7());
    statement.bindString(9, inter.getAttr8());
    statement.bindString(10, inter.getAttr9());
    statement.execute();
    mDatabase.setTransactionSuccessful();
    mDatabase.endTransaction();
   }

public ArrayList<Inter> getAllInter() {
    ArrayList<Inter> listInter = new ArrayList<>();

    String[] columns = {InterHelper.COLUMN_UID,
            InterHelper.COLUMN_ATTR1,
            InterHelper.COLUMN_ATTR2,
            InterHelper.COLUMN_ATTR3,
            InterHelper.COLUMN_ATTR4,
            InterHelper.COLUMN_ATTR5,
            InterHelper.COLUMN_ATTR6,
            InterHelper.COLUMN_ATTR7,
            InterHelper.COLUMN_ATTR8,
            InterHelper.COLUMN_ATTR9
    };
    Cursor cursor = mDatabase.query(InterHelper.TABLE_INTER, columns, null, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {

        do {

            Inter inter = new Inter();

            inter.setAttr1(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR1)));
            inter.setAttr2(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR2)));
            inter.setAttr3(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR3)));
            inter.setAttr4(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR4)));
            inter.setAttr5(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR5)));
            inter.setAttr6(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR6)));
            inter.setAttr7(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR7)));
            inter.setAttr8(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR8)));
            inter.setAttr9(cursor.getString(cursor.getColumnIndex(InterHelper.COLUMN_ATTR9)));
            listInter.add(inter);
            Log.d("Caxx", inter.getAttr1());
        }
        while (cursor.moveToNext());
    }
    return listInter;
}

public void deleteAll() {
    mDatabase.delete(InterHelper.TABLE_INTER, null, null);
}


private static class InterHelper extends SQLiteOpenHelper {
    public static final String TABLE_INTER = "INTER";
    public static final String COLUMN_UID = "_id";
    public static final String COLUMN_ATTR1 = "attr1";
    public static final String COLUMN_ATTR2 = "attr2";
    public static final String COLUMN_ATTR3 = "attr3";
    public static final String COLUMN_ATTR4 = "attr4";
    public static final String COLUMN_ATTR5 = "attr5";
    public static final String COLUMN_ATTR6 = "attr6";
    public static final String COLUMN_ATTR7 = "attr7";
    public static final String COLUMN_ATTR8 = "attr8";

    public static final String COLUMN_ATTR9 = "attr9";

    private static final String CREATE_TABLE_INTER = "CREATE TABLE " + TABLE_INTER + " (" +
            COLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_ATTR1 + " TEXT," +
            COLUMN_ATTR2 + " TEXT," +
            COLUMN_ATTR3 + " TEXT," +
            COLUMN_ATTR4 + " TEXT," +
            COLUMN_ATTR5 + " TEXT," +
            COLUMN_ATTR6 + " TEXT," +
            COLUMN_ATTR7 + " TEXT," +
            COLUMN_ATTR8 + " TEXT," +

            COLUMN_ATTR9 + " TEXT," +

            ");";


    private static final String DB_NAME = "my_db";
    private static final int DB_VERSION = 1;
    private Context mContext;


    public InterHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TABLE_INTER);
            //L.m("create table box office executed");
        } catch (SQLiteException exception) {
            //  L.t(mContext, exception + "");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            //  L.m("upgrade table box office executed");
            db.execSQL(" DROP TABLE " + TABLE_INTER + " IF EXISTS;");
            onCreate(db);
        } catch (SQLiteException exception) {
            // L.t(mContext, exception + "");
        }
    }
}
}

1 个答案:

答案 0 :(得分:2)

你有一个额外的逗号:

COLUMN_ATTR9 + " TEXT," +

会阻止正确创建表格 它应该是:

COLUMN_ATTR9 + " TEXT" +