为什么我收到表名的列不存在错误 [英] Why am i getting a column doesnt exist error with the name of the table

查看:40
本文介绍了为什么我收到表名的列不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi guys Im trying to retrieve some data I inserted into a database in a previous activity. This is my code to Retrieve the data

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'被插入到数据库中.

I really don't understand this error as 'aa1000' was inserted into the database.

这是数据库'train_list'的定义

This is the definition for the database '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"尚未插入数据库的错误.

I Checked the Android Monitor page and i did not find an error to suggest that "aa1000" had not been inserted into the database.

请注意,当只插入Train_nameTrain_no 时,其余都为空.

Please Do note that When inserting only Train_name and Train_no Were filled in the rest were left as null.

任何帮助将不胜感激!

推荐答案

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

Put aa1000 into single quotes ('aa1000'):

您的 SQL 查询应如下所示:

Your SQL query should look like:

Select Train_name From Train_list Where Train_no='aa1000'

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

So modify your select string inside db.rawQuery:

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

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

However it is not advised to use rawQuery for select queries and passing parameters this way, better learn use the query method

这篇关于为什么我收到表名的列不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆