SQLiteLog错误:"没有这样的表"

时间:2015-04-29 07:55:47

标签: android sqlite

我收到了这个错误:

  

没有这样的表格:表格

我正在尝试添加三个JSONJSON_modifiedAdminId字段:

static final String DATABASE_NAME = "db1";
static final int DATABASE_VERSION = 2;
public static final int NAME_COLUMN = 1;

static final String DATABASE_TABLE = "create table " + "adminreg" +
        "( " + "AdminRegID" + " integer primary key autoincrement," + "Rest_name text,Contact_person_name text,text,PASSWORD text,Address text); ";
// Variable to hold the database instance

static final String DATABASE_TABLE_REG="CREATE TABLE IF NOT EXISTS " + " form " +
        "(" + "Id" + " integer primary key autoincrement, " + " JSON text, AdminId text, JSON_modified text);";

public SQLiteDatabase db;

private final Context context;

private DataBaseHelper dbHelper;
public  LoginDatabaseAdapter(Context _context)
{
    context = _context;
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public  LoginDatabaseAdapter open() throws SQLException
{
    db = dbHelper.getWritableDatabase();
    return this;
}

public void close()
{
    db.close();
}

public  SQLiteDatabase getDatabaseInstance()
{
    return db;
}

public void insertEntry(String json, String json_modified)
{
    Log.e("Data Insert reached", "yes");

    ContentValues newValues = new ContentValues();
    Log.e("JSON STRING to be insert", json);
    newValues.put("JSON", json);
    newValues.put("JSON_modified", json_modified);
    newValues.put("AdminId", "1");
    db.insert("form", null, newValues);
    Toast.makeText(context, "Data Is Successfully Saved", Toast.LENGTH_LONG).show();
    Log.e("context", "Toast");
}

DataBaseHelper.java

public DataBaseHelper(Context context, String  name, SQLiteDatabase.CursorFactory factory, int version)
{
    super(context, name, factory, version);
}

// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
    _db.execSQL(LoginDatabaseAdapter.DATABASE_TABLE_REG);
    _db.execSQL(LoginDatabaseAdapter.DATABASE_TABLE);
}

// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
    // Log the version upgrade.
    Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");

    // Upgrade the existing database to conform to the new version. Multiple
    // previous versions can be handled by comparing _oldVersion and _newVersion
    // values.
    // The simplest case is to drop the old table and create a new one.
    _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
    // Create a new one.
    onCreate(_db);
}

3 个答案:

答案 0 :(得分:1)

只需增加数据库版本或重新安装您的应用。

答案 1 :(得分:0)

public class LoginDataBaseAdapter {

    public final static String DATABASE_NAME="db1";
    public final static int VERSION=1;

    public final static String TABLE_NAME="form";   
    private final static String ID="ID";
    private final static String JSON="JSON";
    private final static String JSON_MODIFIED="JSON_MODIFIED";
    private final static String ADMIN_ID="ADMIN_ID";



    public final static String CREATE_TABLE="CREATE TABLE " +TABLE_NAME+ " ("+ID+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+JSON+" TEXT NOT NULL," +
            ""+JSON_MODIFIED+" TEXT NOT NULL,"+ADMIN_ID+" TEXT NOT NULL)";

    public final static String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;


     public Context context;
     public SQLiteDatabase sql_db;
     public DataBaseHelper our_db_helper;

     public LoginDataBaseAdapter(Context context)
        {
            this.context=context;
        }

    public void open() {
            our_db_helper=new DataBaseHelper(context);
            sql_db=our_db_helper.getWritableDatabase();

        }

    public void close() {

            sql_db.close();

        }

    public long dataInsert(String json,String modified_json, String admin_id)   
    {
        try{

                ContentValues data=new ContentValues();

                data.put(JSON,json);
                data.put(JSON_MODIFIED, modified_json);
                data.put(ADMIN_ID, admin_id);

                return  sql_db.insert(TABLE_NAME, null, data);
        }

        catch(Exception ee)
        {

        }
        return -1;
    }


    public String getData()
    {
        try{

                String[] columns=new String[]{ID,JSON,JSON_MODIFIED,ADMIN_ID};
                Cursor content=sql_db.query(TABLE_NAME, columns, null, null, null, null, null);

                int index_id=content.getColumnIndex(ID);
                int index_json=content.getColumnIndex(JSON);
                int index_json_modifier=content.getColumnIndex(JSON_MODIFIED);
                int index_admin_id=content.getColumnIndex(ADMIN_ID);

                String data="";

                if(content==null)
                    return data;

                    for(content.moveToFirst();!content.isAfterLast();content.moveToNext())
                    {


                        data=data+ "Id: "+content.getInt(index_id)+" JSON: "+content.getString(index_json)+
                                " Modified JSON: "+content.getString(index_json_modifier)+" Admin_id: "+content.getString(index_admin_id)+"\n";


                    }

                    return data;

        }catch(Exception ee){
            return "";
        }
    }

}


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper
    {
         public DataBaseHelper(Context context) {       
            super(context, LoginDataBaseAdapter.DATABASE_NAME, null, LoginDataBaseAdapter.VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(LoginDataBaseAdapter.CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(LoginDataBaseAdapter.DROP_TABLE);
            onCreate(db);
        }
    }   

<br>

现在您可以使用任何类的代码......
例如:
(1)ToInsertData:

LoginDataBaseAdapter db=new LoginDataBaseAdapter(YOUR_CLASS_NAME.this);
               db.open();
               long response= db.dataInsert("a", "abd", "abcd");
               db.close();
               if(response!=-1)
                   Toast.makeText(YOUR_CLASS_NAME.this, "Data Inserted...", Toast.LENGTH_LONG).show();
               else
                   Toast.makeText(YOUR_CLASS_NAME.this, "Some Error", Toast.LENGTH_LONG).show();


(2)ToFetchData:

 LoginDataBaseAdapter db=new LoginDataBaseAdapter(YOUR_CLASS_NAME.this);
               db.open();
               Toast.makeText(YOUR_CLASS_NAME.this, db.getData().toString(), Toast.LENGTH_LONG).show();
               db.close();

答案 2 :(得分:0)

您必须卸载该应用程序,然后重新安装。它应该在那之后工作。您的设备上可能有一个旧版本的数据库,它具有(空)数据库。