android.database.sqlite.SQLiteException:

我正在研究这个项目,如果应用程序处于离线状态,数据将被保存到SQLite。但是当我构建和运行时,我得到这个错误。我也重新检查了SQL查询。但无法得到确切的问题是什么。


主要活动.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 ;



UYOU
浏览 150回答 3
3回答

手掌心

此行生成语法错误的 sql 语句:String&nbsp;selection=DbContract.NAME+"LIKE?";如果将类似“Bob”的值作为参数传递,则结果将为:(而不是 ),并将被视为列名。nameLIKEBobname LIKE 'Bob'nameLIKEBob您需要像这样插入空格:String&nbsp;selection=DbContract.NAME+"&nbsp;LIKE&nbsp;?";

ITMISS

我遇到了同样的问题,但它是从表中删除一列,所以当我更改它时db.delete(cons.tableNames[3], Cart.KEY_f+"=$valueToChange", null)为此db.delete(cons.tableNames[3], Cart.KEY_f + "&nbsp; LIKE&nbsp; '%" + valueToChange + "%' ", null)我像这样做更新fun updateCart(id: Int, mBusiness: Business) {&nbsp; &nbsp; val db = dbHelper.writableDatabase&nbsp; &nbsp; // New value for one column&nbsp; &nbsp; val valueToChange = mBusiness.e&nbsp; &nbsp; val values = ContentValues().apply {&nbsp; &nbsp; &nbsp; &nbsp; put(Business.KEY_e, valueToChange)&nbsp; &nbsp; }&nbsp; &nbsp; db.update(cons.tableNames[p.mReturnIntSP(meuContexto, cons.tablePosition)], values, "id=$id", null)&nbsp; &nbsp; db.close() // Closing database connection}

POPMUISE

将数据库版本从 1 更改为 201。它现在正在工作。我实际上不知道版本号与解决方案有什么关系。希望有人给出一个解释。干杯。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java