android - 为什么我收到表名称的列不存在错误

标签 android mysql sqlite android-studio

大家好,我正在尝试检索我在之前的 Activity 中插入数据库的一些数据。这是我检索数据的代码

public class Display_data extends AppCompatActivity {
    ArrayList<String> Displist = new ArrayList<String>();
    ListView no_name;
    String db_Name;
    TextView namer;
    TextView Starter;
    TextView Ender;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display_data);
            Bundle bundle = getIntent().getExtras();
            no_name = (ListView) findViewById(R.id.Disp_list);
            String message = bundle.getString("package");
            String parameter;
            SQLiteDatabase db = openOrCreateDatabase(message, SQLiteDatabase.CREATE_IF_NECESSARY, null);
            SQLiteDatabase db1 = openOrCreateDatabase("Train_list.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
            Cursor header;
            namer=(TextView)findViewById(R.id.T1);
            try {
                header = db1.rawQuery("Select Train_name From Train_list Where Train_no="+message.substring(0,(message.length()-3)), null);
                header.moveToFirst();
                String temp = header.getString((header.getColumnIndex("Train_name")));
                Toast.makeText(Display_data.this, "blahhh:"+temp, Toast.LENGTH_LONG).show();
                Log.d("Sucess!","temp:"+temp);
                Toast.makeText(Display_data.this, "Gotcha:"+temp , Toast.LENGTH_LONG).show();
                namer.setText("Train Name: " + temp);
            }catch(Exception e)
            {
                Log.d("Final Error",e.toString());
                Toast.makeText(Display_data.this, "Error", Toast.LENGTH_LONG).show();
            }

        db_Name=message.substring(0,(message.length()-3));
        try{
            Cursor data_fetch = db.rawQuery("Select Stops From "+db_Name, null);
            while (data_fetch.moveToNext()) {
                String name = data_fetch.getString(data_fetch.getColumnIndex("Stops"));
                if(Displist.contains(name)) {

                }
                else {
                    Displist.add(name);
                    //Toast.makeText(Display_data.this, "Added :"+name, Toast.LENGTH_SHORT).show();

                }

            }
            data_fetch.close();
        }catch (Exception e)
        {
            Toast.makeText(Display_data.this, "Yeah"+e.toString(), Toast.LENGTH_LONG).show();
            Log.d("Damn Why",e.toString());

        }
        try {

            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_list_item_1,Displist);
            no_name.setAdapter(arrayAdapter);
        }catch (Exception e){
            Log.d("Display data error ",e.toString());
            Toast.makeText(Display_data.this, "Listview Error" , Toast.LENGTH_LONG).show();
        }

    }
}

我注意到该部分

try {
            header = db1.rawQuery("Select Train_name From Train_list Where Train_no="+message.substring(0,(message.length()-3)), null);
            header.moveToFirst();
            String temp = header.getString((header.getColumnIndex("Train_name")));
            Toast.makeText(Display_data.this, "blahhh:"+temp, Toast.LENGTH_LONG).show();
            Log.d("Sucess!","temp:"+temp);
            Toast.makeText(Display_data.this, "Gotcha:"+temp , Toast.LENGTH_LONG).show();
            namer.setText("Train Name: " + temp);
        }catch(Exception e)
        {
            Log.d("Final Error",e.toString());
            Toast.makeText(Display_data.this, "Error", Toast.LENGTH_LONG).show();
        }

返回错误

D/Final Error: android.database.sqlite.SQLiteException: no such column: aa1000 (code 1): , while compiling: Select Train_name From Train_list Where Train_no=aa1000

我真的不明白这个错误,因为“aa1000”已插入数据库。

这是数据库“train_list”的定义

 SQLiteDatabase db = openOrCreateDatabase( "Train_list.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
    try {
        final String CREATE_TABLE_TRAIN_LIST = "CREATE TABLE IF NOT EXISTS Train_list ("
                + "Train_name VARCHAR,"
                + "Train_no VARCHAR,"
                + "Train_start VARCHAR,"
                + "Train_end VARCHAR,"
                + "Seats_Available VARCHAR);";
        db.execSQL(CREATE_TABLE_TRAIN_LIST);
        Toast.makeText(admin_manipulation.this, "Table created", Toast.LENGTH_LONG).show();
        String sql = "INSERT or replace INTO Train_list (Train_name, Train_no, Train_start, Train_end, Seats_Available) VALUES('"+str_Train_name + "',' " +str_Train_no + "', '" +str_Train_start+"','" +str_Train_end+"',' " +str_Train_seats +"');";
        try {
            db.execSQL(sql);
            Toast.makeText(admin_manipulation.this, "train no:"+str_Train_no, Toast.LENGTH_SHORT).show();
        }catch(Exception e)
        {
            Toast.makeText(admin_manipulation.this, "Sorry Not Inserted Sucessfully", Toast.LENGTH_SHORT).show();
            Log.d("Error experienced",e.toString());
        }

我检查了 Android Monitor 页面,没有发现错误提示“aa1000”尚未插入数据库。

请注意,插入时仅填写Train_nameTrain_no,其余部分保留为空。

如有任何帮助,我们将不胜感激!

最佳答案

aa1000 放入单引号中 ('aa1000'):

您的 SQL 查询应如下所示:

Select Train_name From Train_list Where Train_no='aa1000'

因此修改 db.rawQuery 中的选择字符串:

"Select Train_name From Train_list Where Train_no='"+message.substring(0,(message.length()-3))+"'";

但是,不建议使用 rawQuery 进行选择查询并以这种方式传递参数,最好学习使用 query方法

关于android - 为什么我收到表名称的列不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47157827/

相关文章:

C++ SQLite3 如何知道选择是否返回 0 行

android - Paypal Android SDK 是否支持并行、链式支付?

java - UDP 数据包发现不起作用

mysql - 从 Node js 加载数据 infile 执行缓慢

mysql - sqljocky 查询 - 未找到方法

mysql - 外键引用另一个表中的多行

android - 将房间数据库导出到 json 文件

node.js - 使用 Sequelize 和 Sqlite3 设置多对多关联

android - ViewPager 的 instantiateItem 中的类转换异常

android - 开始使用 Honeycomb 进行 Android 开发