mysqlite数据库中没有列

时间:2015-05-30 08:09:46

标签: java android eclipse sqlite

我做了一个使用sqlite作为数据库的android eclipse项目,当我尝试在手机中运行项目时会发生一些错误。当我点击查看按钮(查看我的数据库)时,它说我的数据库中没有列,有人可以帮助我为什么会这样。

这是我的编码 数据库文件:

public class activityMain extends ActionBarActivity {

    private final static String TAG = activityMain.class.getSimpleName();
    Ruta[] mRute;

    @InjectView(R.id.textView) TextView text1;
    @InjectView(R.id.textView2) TextView text2;
    @InjectView(R.id.textView3) TextView text3;

    String redditUrl = "http://localhost/nadjiprijevoz/connect.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mainmenu);
        ButterKnife.inject(this);

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(redditUrl)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {

                    String jsonData = response.body().string();
                    Log.i(TAG, response.body().string());

                    if (response.isSuccessful()) {

                        mRute = parsePostDetails(jsonData);

                        text1.setText(mRute[0].getNaziv());
                        text2.setText(mRute[0].getPolaziste());
                        text3.setText(mRute[0].getOdrediste());

                    }
                } catch (IOException e) {
                    Log.e(TAG, "Excepiton caught: " , e);
                } catch (JSONException e) {
                    Log.e(TAG, "Excepiton caught: " , e);
                }

            }
        });

    }

    private Ruta[] parsePostDetails(String jsonData) throws JSONException {

        JSONObject rutaObjectJSON = new JSONObject(jsonData);
        JSONArray rutaArrayJSON = rutaObjectJSON.getJSONArray("rute");

        Ruta[] ruteTemp = new Ruta[rutaArrayJSON.length()];

        for (int i = 0; i < ruteTemp.length; i++) {

            JSONObject rutaDetailJSON = rutaArrayJSON.getJSONObject(i);

            Ruta tempRuta = new Ruta();

            tempRuta.setNaziv(rutaDetailJSON.getString("naziv"));
            tempRuta.setOdrediste(rutaDetailJSON.getString("odrediste"));
            tempRuta.setPolaziste(rutaDetailJSON.getString("polaziste"));

            ruteTemp[i] = tempRuta;

        }

        return ruteTemp;
    }


}

主要Activity.java

public class Translator {

public static final String KEY_ROWID ="_id";
public static final String KEY_DESC ="page_desc";

private static final String DATABASE_NAME  ="Translatordb";
private static final String DATABASE_TABLE ="pageTable";
private static final int DATABASE_VERSION  = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

public static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {  
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID + " STRING PRIMARY KEY, " +
        KEY_DESC + " TEXT NOT NULL);"
                );
    }


    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

 public Translator(Context c){
     ourContext = c;
}

public Translator open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

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

public long createEntry(String id, String description){
    ContentValues cv = new ContentValues();
    cv.put(KEY_ROWID,id);
    cv.put(KEY_DESC,description);
    return ourDatabase.insert(DATABASE_TABLE,null, cv);
}

public long createEntry1(String id, String description){
    ContentValues cv = new ContentValues();
    cv.put(KEY_ROWID,"P01");
    cv.put(KEY_DESC,"Snow White");
    return ourDatabase.insert(DATABASE_TABLE,null, cv);
}


public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID,KEY_DESC};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns,null,null,null,null,null);
    String result =" ";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iDescription = c.getColumnIndex(KEY_DESC);

 for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    result =result + c.getString(iRow)+ "  " +c.getString(iDescription) +"  \n";
    }

    return result;

}   

SQLView.java

public class MainActivity extends Activity implements OnClickListener {


static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
Button view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view =(Button) findViewById(R.id.viewSQL);
    view.setOnClickListener(this);

}



  //product qr code mode
public void scanQR(View v) {
    try {
        //start the scanning activity from the com.google.zxing.client.android.SCAN intent
        Intent intent = new Intent(ACTION_SCAN);
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);


    } catch (ActivityNotFoundException anfe) {
        //on catch, show the download dialog
        showDialog(MainActivity.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
}

//alert dialog for downloadDialog
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
    downloadDialog.setTitle(title);
    downloadDialog.setMessage(message);
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                act.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {

            }
        }
    });
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return downloadDialog.show();
}

//on ActivityResult method
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
            toast.show();
        }
    }
}

public void onClick(View arg0) {
    switch(arg0.getId()){

        case R.id.viewSQL:   

            Translator entry = new Translator(MainActivity.this);
            entry.open();
            entry.close();
            Intent i = new Intent("com.example.scanner.SQLVIEW");
            startActivity(i);
            break;
        }
    }

这是我的错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.scanner / com.example.scanner.SQLView}:android.database.sqlite.SQLiteException:no such column:page_desc(code 1):, while编译:SELECT _id,page_desc FROM pageTable

有人能告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

试试这个

public class Translator extends SQLiteOpenHelper {
private static final String LOGCAT = null;

public Translator(Context applicationcontext) {
    super(applicationcontext, "androidsqlite.db", null, 1);
    Log.d(LOGCAT, "Created Translator Database");
}

public static final String KEY_ROWID = "_id";
public static final String KEY_DESC = "page_desc";

private static final String DATABASE_TABLE = "pageTable";

private SQLiteDatabase ourDatabase;

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
            + " STRING PRIMARY KEY, " + KEY_DESC + " TEXT NOT NULL);");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    onCreate(db);
}

public long createEntry(String id, String description) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_ROWID, id);
    cv.put(KEY_DESC, description);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public long createEntry1(String id, String description) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_ROWID, "P01");
    cv.put(KEY_DESC, "Snow White");
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[] { KEY_ROWID, KEY_DESC };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
            null, null);
    String result = " ";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iDescription = c.getColumnIndex(KEY_DESC);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iRow) + "  "
                + c.getString(iDescription) + "  \n";
    }

    return result;

}
}