无法将列动态添加到sqlite表中

时间:2013-08-12 16:21:48

标签: android sqlite

logcat 表示以下错误:

08-12 21:31:12.373: I/Database(325): sqlite returned: error code = 1, msg = near "10010325": syntax error
08-12 21:31:12.373: E/Database(325): Failure 1 (near "10010325": syntax error) on 0x2396b8 when preparing 'ALTER TABLE Student_attendance ADD COLUMN 10010325 INTEGER NOT NULL DEFAULT 0;'.

这是 StudentLoginPage.java 当学生创建新帐户时addcolumn调用该函数,该函数应向ATTENDANCE_TABLE添加新列

case R.id.buttonRegister:

    mStudentname = (EditText) findViewById(R.id.editStudentname);
    mSPassword = (EditText) findViewById(R.id.editSPassword);
    mSRollno = (EditText) findViewById(R.id.editRoll);
    mSEmail = (EditText) findViewById(R.id.editEmail);

    String uname = mStudentname.getText().toString();
    String pass = mSPassword.getText().toString();
    String roll = mSRollno.getText().toString();
    String email = mSEmail.getText().toString();
    boolean invalid = false;

        if (uname.equals("")) {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Username Missing",
                    Toast.LENGTH_SHORT).show();
        } else if (pass.equals("")) {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Password Missing",
                    Toast.LENGTH_SHORT).show();
        } else if (roll.equals("")) {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Roll no Missing",
                    Toast.LENGTH_SHORT).show();
        } else if (email.equals("")) {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Email ID Missing",
                    Toast.LENGTH_SHORT).show();
        }

        if (invalid == false) {
            addEntry(uname, pass, roll, email);
            addColumn(roll);
            Intent i_register = new Intent(NewStudentPage.this,
                    MainActivity.class);
            startActivity(i_register);
            finish();
        }

        break;
    }
}

private void addColumn(String roll) {
    // TODO Auto-generated method stub
    SQLiteDatabase db = myDb.getWritableDatabase();
    try {
        db.execSQL("ALTER TABLE "+ DbHelper.ATTENDANCE_TABLE +" ADD COLUMN "+ roll +" INTEGER NOT NULL DEFAULT 0;");
    } catch (Exception e) {
        // TODO: handle exception
    }

}

这是 DbHelper.java 在这里,我创建了三个表STUDENT_TABLE,ATTENDANCE_TABLE和TEACHER_TABLE

public class DbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "subh.db";
    private static final int DATABASE_VERSION = 1;
    public static final String ATTENDANCE_TABLE = "Student_attendance";
    public static final String TEACHER_TABLE = "Teacher_login";
    public static final String STUDENT_TABLE = "Student_login";
    public static final String T_ROW_ID = "teacher_id";

    private static final String ATTENDANCE_TABLE_CREATE = "CREATE TABLE "
            + ATTENDANCE_TABLE + "(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "teacher_id INTEGER , " + "time TEXT NOT NULL); ";

    private static final String TEACHER_TABLE_CREATE = "CREATE TABLE "
            + TEACHER_TABLE
            + "( teacher_id INTEGER PRIMARY KEY AUTOINCREMENT , "
            + "t_name TEXT NOT NULL , t_pass TEXT NOT NULL , t_email TEXT NOT NULL);";

    private static final String STUDENT_TABLE_CREATE = "CREATE TABLE "
            + STUDENT_TABLE + "( s_id INTEGER PRIMARY KEY AUTOINCREMENT , "
            + "s_name TEXT NOT NULL , s_pass TEXT NOT NULL , "
            + "s_roll_no TEXT NOT NULL , " + "s_email TEXT NOT NULL);";

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        System.out.println("In constructor");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            db.execSQL(TEACHER_TABLE_CREATE);
            db.execSQL(ATTENDANCE_TABLE_CREATE);
            db.execSQL(STUDENT_TABLE_CREATE);

            System.out.println("In onCreate");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Log the version upgrade.
        Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");


            db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");

        // Create a new one.
        onCreate(db);

    }

}

1 个答案:

答案 0 :(得分:3)

  

ADD COLUMN 10010325 ...

列名必须是有效的标识符 - 以拉丁字母开头的字母数字,但是您的列名称为10010325 - 这不起作用!

<强>更新

动态添加列听起来像灾难的配方。使用适当的数据库模式会更好,例如:

CREATE TABLE classes (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    class_name VARCHAR(32),
    teacher_id INTEGER,
    time TEXT NOT NULL
);

然后创建这样的考勤表:

CREATE TABLE student_attendance (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    class_id INTEGER,
    student_id INTEGER
);

student_attendance现在应该为每个参加课程的学生提供额外的行。 这样,您就不必动态更改架构。