开发数据库时出错

时间:2017-04-04 14:01:18

标签: java android android-sqlite

我制作了一个Android数据库项目。代码是:

package ir.applinkfinder.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "student.db";
public static final String TABLE_NAME = "student_table";

public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null,  1);
    //SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
}    
public boolean insertData(String name, String surname, String marks){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,name); // put(column name which you want to insert data, value itself)
    contentValues.put(COL_3,surname);
    contentValues.put(COL_4,marks);

    long MyResult = db.insert(TABLE_NAME, null, contentValues); 

    if( MyResult == -1 )
        return false;
    else
        return true;
}

但错误是“错误:类,接口或枚举预期+ android studio数据库”这与insertData方法有关,有人可以帮我吗?

1 个答案:

答案 0 :(得分:4)

}方法上方有一个迷路insertData字符,请将其删除。