繁体   English   中英

在SQLite中创建数据库时出错

[英]Error while creating a Database in SQLite

我在将数据放入SQLite时遇到问题,我接近“命令”:语法错误(代码1):,而在编译时:CREATE TABLE position_arret(id INTEGER PRIMARY KEY,order TEXT,libelle TEXT,posx TEXT,posy TEXT)

我有以下课程

定位站分类为

public class Position_Station {

        //variables
        int _id;
        String _PosX;
        String _PosY;
        String _Order;
        String _Libelle;


        // Empty constructor
        public Position_Station(){}
        // constructor
        public Position_Station(int id,String order, String libelle, String posx, String posy){
            this._id = id;
            this._Libelle=libelle;
            this._Order=order;
            this._PosX = posx;
            this._PosY=posy;

        }

        // constructor
        public Position_Station(String order, String libelle, String posx, String posy){
            this._Libelle=libelle;
            this._Order=order;
            this._PosX = posx;
            this._PosY=posy;
        }


        // getting ID SQLITE/////////////////////////
        public int getID(){
            return this._id;
        }

                    // setting ID SQLITE
                    public void setID(int id){
                        this._id = id;
                    }


        // getting Libelle//////////////////////////////
        public String getLibelle(){
            return this._Libelle;
        }

                    // setting Libelle
                    public void setLibelle(String libelle){
                        this._Libelle = libelle;
                    }


        // getting Order//////////////////////////////
        public String getOrder(){
            return this._Order;
        }

                    // setting Order
                    public void setOrder(String order){
                        this._Order = order;
                    }                   


        // getting PosX//////////////////////////////
        public String getPosX(){
            return this._PosX;
        }

                    // setting PosX
                    public void setPosX(String posx){
                        this._PosX = posx;
                    }

        // getting PosY///////////////////////////
        public String getPosY(){
            return this._PosY;
        }

                    // setting PosY
                    public void setPosY(String posy){
                        this._PosY = posy;
                    }
    }

我的DatabaseHandler类为

public class DatabaseHandlerStation extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 2;

    // Database Name
    private static final String DATABASE_NAME = "POSITIONSSTATIONMANAGER";

    // Contacts table name
    private static final String TABLE_POSITIONS = "position_arret";

    // Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_LIBELLE = "libelle";
    private static final String KEY_ORDER="order";
    private static final String KEY_POSX = "posx";
    private static final String KEY_POSY="posy";

    public DatabaseHandlerStation(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_POSITIONS + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_ORDER + " TEXT,"
                + KEY_LIBELLE + " TEXT,"
                + KEY_POSX + " TEXT,"
                + KEY_POSY + " TEXT" +")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_POSITIONS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
    //delete all rows in table
    public void deleteAll()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_POSITIONS,null,null);
        //   db.execSQL("DROP TABLE IF EXISTS " + TABLE_POSITIONS);
        //   db.execSQL("TRUNCATE TABLE " + TABLE_POSITIONS);
        db.close();
    }

    // Adding new position
    void addPosition(Position_Station p_station) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ORDER, p_station.getOrder());
        values.put(KEY_LIBELLE, p_station.getLibelle());
        values.put(KEY_POSX, p_station.getPosX()); // Laltitude Pos x
        values.put(KEY_POSY, p_station.getPosY()); // Longitude Pos y


        // Inserting Row
        db.insert(TABLE_POSITIONS, null, values);
        db.close(); // Closing database connection
    }



    // Getting All Positions
    public List<Position_Station> getAllPositions() {
        List<Position_Station> positionList = new ArrayList<Position_Station>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_POSITIONS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Position_Station pos_station = new Position_Station();
                pos_station.setID(Integer.parseInt(cursor.getString(0)));
                pos_station.setOrder(cursor.getString(1));
                pos_station.setLibelle(cursor.getString(2));
                pos_station.setPosX(cursor.getString(3));
                pos_station.setPosX(cursor.getString(4));
                // Adding contact to list
                positionList.add(pos_station);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        // return contact list
        return positionList;
    }

    // Updating single position
    public int updatePosition_Station(Position_Station position) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ORDER, position.getOrder());
        values.put(KEY_LIBELLE, position.getLibelle());
        values.put(KEY_POSX, position.getPosX());
        values.put(KEY_POSY, position.getPosY());

        // updating row
        return db.update(TABLE_POSITIONS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(position.getID()) });
    }

    // Deleting single position
    public void deletePosition(Position_Station position) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_POSITIONS, KEY_ID + " = ?",
                new String[] { String.valueOf(position.getID()) });
        db.close();
    }

    // Getting Positions Count
    public int getPositionsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_POSITIONS;
        int i;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        i=cursor.getCount();
        cursor.close();
        // return count
        return i;   
    }



    // Getting all posx and posy 
    public ArrayList<LatLng> getAllPos() {
        ArrayList<LatLng> coordList = new ArrayList<LatLng>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_POSITIONS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                //  Position position = new Position();
                //  position.setPosX(cursor.getString(1));
                //  position.setPosY(cursor.getString(2));
                // Adding position to list
                coordList.add(new LatLng(Double.parseDouble(cursor.getString(3))
                        ,Double.parseDouble(cursor.getString(4))));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        // return contact list
        return coordList;
    }

    // Getting all posx, posy, and information
    public ArrayList<Position_Station> getAllPos_info() {
        ArrayList<Position_Station> coordList = new ArrayList<Position_Station>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_POSITIONS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Position_Station pos_station = new Position_Station();
                pos_station.setOrder(cursor.getString(1));
                pos_station.setLibelle(cursor.getString(2));
                pos_station.setPosX(cursor.getString(3));
                pos_station.setPosY(cursor.getString(4));
                // Adding position to list
                coordList.add(pos_station);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        // return contact list
        return coordList;
    }
}

我的MainActivity就像我想将数据放入数据库时

DatabaseHandlerStation db_pos_station= new DatabaseHandlerStation(this);

JSONArray jsonArret = new JSONArray(Data);
                        for(int i=0;i < jsonArret.length();i++) {
                            JSONObject jsonobj = jsonArret.getJSONObject(i);
                            String order=jsonobj.getString(TAG_ORDER);
                            String libel=jsonobj.getString(TAG_LIBELLE);
                            String posx=jsonobj.getString(TAG_POSX_STATION);
                            String posy=jsonobj.getString(TAG_POSY_STATION);
                            db_pos_station.addPosition(new Position_Station(order,libel,posx,posy));  

谢谢

order是一个SQL关键字。 重命名该列,或将其用"double quotes"

order是保留的sql单词,重命名该条目,它将可以正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM