android.database.sqlite.SQLiteException:无此类列:名称(代码1)

时间:2019-03-10 14:10:05

标签: java android sql sqlite

我正在从事这个项目,如果应用程序离线,数据将保存到SQLite。但是我在构建和运行时遇到此错误。我也用SQL查询进行了检查。但是无法得到确切的问题。

MainActivity.java

public class MainActivity extends AppCompatActivity {


    RecyclerView recyclerView;
    EditText name;
    RecyclerView.LayoutManager layoutManager;
    RecyclerAdapter recyclerAdapter;
    ArrayList<Contact> arrayList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recyclerView);
        name=findViewById(R.id.name);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerAdapter=new RecyclerAdapter(arrayList);
        recyclerView.setAdapter(recyclerAdapter);
        readfromlite();
    }
private void readfromlite()
    {
        arrayList.clear();
        DbHelper dbHelper=new DbHelper(this);
        SQLiteDatabase database=dbHelper.getReadableDatabase();
        Cursor cursor=dbHelper.readlite(database);
         while (cursor.moveToNext())
         {

             String name=cursor.getString(cursor.getColumnIndex(DbContract.NAME));
             int sync_status= cursor.getInt(cursor.getColumnIndex(DbContract.sync_status));
             arrayList.add(new Contact(name,sync_status));
         }

        recyclerAdapter.notifyDataSetChanged();
         cursor.close();
         dbHelper.close();
}

DBHelper.java

public class DbHelper extends SQLiteOpenHelper {
    private static final int dbversion=1;
    private static final String ctable= " create TABLE if not EXISTS " + DbContract.tname + " (id integer primary key autoincrement, " + DbContract.NAME + " text , " + DbContract.sync_status + " integer ) ; " ;
    private static final String dtable= " drop table if EXISTS " + DbContract.tname ;

    public DbHelper(Context context){
    super(context,DbContract.dbname,null,dbversion);


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

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL(dtable);
    onCreate(db);

    }
    public void savelite(String name,int sync_status,SQLiteDatabase database){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DbContract.NAME,name);
        contentValues.put(DbContract.sync_status,sync_status);
        database.insert(DbContract.tname,null,contentValues);
    }

    public Cursor readlite(SQLiteDatabase database){

        String[] resultset={DbContract.NAME,DbContract.sync_status};
    return (database.query(DbContract.tname,resultset,null,null,null,null,null));
    }

    public void updatelite(String name,int sync_status,SQLiteDatabase database){

        ContentValues contentValues=new ContentValues();
        contentValues.put(DbContract.sync_status,sync_status);
        String selection=DbContract.NAME+"LIKE?";
        String[] selection_args={name};
        database.update(DbContract.tname,contentValues,selection,selection_args);

    }
}

DbContract.java

public class DbContract {

    public static final int SYNC_STATUS_OK=0;
    public static final int SYNC_STATUS_FAILED=1;
    public static final String dbname="contactdb";
    public static final String tname="contactinfo";
    public static final String NAME="name";
    public static final String sync_status="syncstatus";

}

Logcat

2019-03-10 19:22:31.438 2946-2946/com.app.datamanager E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.datamanager, PID: 2946
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.datamanager/com.app.datamanager.MainActivity}: android.database.sqlite.SQLiteException: no such column: name (code 1): , while compiling: SELECT name, syncstatus FROM contactinfo
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3012)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:232)
        at android.app.ActivityThread.main(ActivityThread.java:6802)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
     Caused by: android.database.sqlite.SQLiteException: no such column: name (code 1): , while compiling: SELECT name, syncstatus FROM contactinfo
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:965)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:576)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1353)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1200)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1071)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1239)
        at com.app.datamanager.DbHelper.readlite(DbHelper.java:41)
        at com.app.datamanager.MainActivity.readfromlite(MainActivity.java:52)
        at com.app.datamanager.MainActivity.onCreate(MainActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:6974)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3012) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:232) 
        at android.app.ActivityThread.main(ActivityThread.java:6802)

3 个答案:

答案 0 :(得分:0)

此行产生语法错误的sql语句:

String selection=DbContract.NAME+"LIKE?";

如果将“ Bob”之类的值作为参数传递,结果将是:
nameLIKEBob(而不是name LIKE 'Bob')和nameLIKEBob被视为列名。

您需要插入这样的空格:

String selection=DbContract.NAME+" LIKE ?";

答案 1 :(得分:0)

  

将数据库版本从1更改为201。

现在正在工作。 我实际上不知道该解决方案与哪个版本号有关。希望有人给出解释。干杯。

答案 2 :(得分:0)

我遇到了同样的问题,但是那是因为从表中删除了列,所以当我更改它时

db.delete(cons.tableNames[3], Cart.KEY_f+"=$valueToChange", null)

为此

db.delete(cons.tableNames[3], Cart.KEY_f + "  LIKE  '%" + valueToChange + "%' ", null)

我会这样更新

fun updateCart(id: Int, mBusiness: Business) {
    val db = dbHelper.writableDatabase
    // New value for one column
    val valueToChange = mBusiness.e
    val values = ContentValues().apply {
        put(Business.KEY_e, valueToChange)
    }

    db.update(cons.tableNames[p.mReturnIntSP(meuContexto, cons.tablePosition)], values, "id=$id", null)
    db.close() // Closing database connection
}