Android Sqlite插入错误

时间:2016-04-25 10:13:59

标签: android android-sqlite

当我想在此表中插入记录时,我在Sqlite数据库中创建了一个表,我遇到错误没有列名列column_name。

这是我的Message.java类:

public class Messages {
private int id = 0;
private String message = "";
private String type = "0";
private String geolat = "";
private String geolng = "";
public Messages(){

}

public Messages(int id,String message,String type,String geolat,String geolng) {
    this.id = id;
    this.message = message;
    this.type = type;
    this.geolat = geolat;
    this.geolng = geolng;
}
public Messages(String message,String type,String geolat,String geolng) {
    this.message = message;
    this.type = type;
    this.geolat = geolat;
    this.geolng = geolng;
}

public String getGeolng() {
    return geolng;
}

public String getGeolat() {
    return geolat;
}

public String getType() {
    return type;
}

public String getMessage() {
    return message;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public void setMessage(String message) {
    this.message = message;
}

public void setType(String type) {
    this.type = type;
}

public void setGeolat(String geolat) {
    this.geolat = geolat;
}

public void setGeolng(String geolng) {
    this.geolng = geolng;
}
}

这是MessageDatabaseHandler.java:

public class MessageDatabaseHandler extends SQLiteOpenHelper {

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

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

// Contacts table name
private static final String TABLE_MESSAGE = "messages";

//table fields
private static final String id = "id";
private static final String message = "message";
private static final String type = "type";
private static final String geolat = "geolat";
private static final String geolng = "geolng";


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

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_MESSAGE_TABLE = "CREATE TABLE " + TABLE_MESSAGE + "("

            + id + " INTEGER PRIMARY KEY,"
            + message + " TEXT,"
            + type + " TEXT"
            + geolat +" TEXT"
            + geolng + " TEXT"

            +")";
    db.execSQL(CREATE_MESSAGE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);
    // Create tables again
    onCreate(db);

}

//ADD NEW
public void addNewMessage(Messages messages){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(message, messages.getMessage());
    values.put(type, messages.getType());
    values.put(geolat, messages.getGeolat());
    values.put(geolng, messages.getGeolng());

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

// Getting single contact
Messages getMessage(int inputId) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_MESSAGE, new String[]
                    {
                            id,
                            message,
                            type,
                            geolat,
                            geolng
                    },
                    id + "=?",

            new String[] { String.valueOf(inputId) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Messages messages = new Messages(
                            Integer.parseInt(cursor.getString(0)),
                            cursor.getString(1),
                            cursor.getString(2),
                            cursor.getString(3),
                            cursor.getString(4)
    );
    // return contact
    return messages;
}

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

// Getting All Messages
public List<Messages> getAllMessages() {
    List<Messages> messageList = new ArrayList<Messages>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_MESSAGE;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Messages message = new Messages();
            message.setId(Integer.parseInt(cursor.getString(0)));
            message.setMessage(cursor.getString(1));
            message.setType(cursor.getString(2));
            message.setGeolat(cursor.getString(3));
            message.setGeolng(cursor.getString(4));
            // Adding message to list
            messageList.add(message);
        } while (cursor.moveToNext());
    }

    // return message list
    return messageList;
}

// Deleting single contact
public void deleteAllMessages(Messages message) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_MESSAGE,null,null);
    db.close();
}
}

这就是我的活动中插入行的方式:

MessageDatabaseHandler db = new MessageDatabaseHandler(MainActivity.this);
                // Inserting Message
                Log.d("Insert: ", "Inserting ..");
                db.addNewMessage(new Messages(
                                            "",
                                            "0",
                                            "99999",
                                            "88888"
                        )
                );

最后这个错误: Site isolation project

请提前帮助我解决这个问题。

2 个答案:

答案 0 :(得分:2)

像这样更改创建查询。

String CREATE_MESSAGE_TABLE = "CREATE TABLE " + TABLE_MESSAGE + "("
            + id + " INTEGER PRIMARY KEY, "
            + message + " TEXT, "
            + type + " TEXT, "
            + geolat +" TEXT, "
            + geolng + " TEXT)";

答案 1 :(得分:1)

我认为你在onCreate()中遗漏了一些逗号。尝试按以下方式更改它。

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_MESSAGE_TABLE = "CREATE TABLE " + TABLE_MESSAGE + "("

            + id + " INTEGER PRIMARY KEY,"
            + message + " TEXT,"
            + type + " TEXT,"
            + geolat +" TEXT,"
            + geolng + " TEXT"

            +")";
    db.execSQL(CREATE_MESSAGE_TABLE);

}