简体   繁体   English

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

[英]Unable to add column dynamically into the sqlite table

The logcat says following error: 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;'.

Here is the StudentLoginPage.java when a student makes a new account addcolumn function is called which should add a new column to the ATTENDANCE_TABLE 当学生创建一个新帐户addcolumn函数时,这是StudentLoginPage.java ,它应该向addcolumn添加一个新列

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
    }

}

Here's the DbHelper.java Here I have created three tables STUDENT_TABLE,ATTENDANCE_TABLE and TEACHER_TABLE 这是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);

    }

}

ADD COLUMN 10010325 ... 添加栏目10010325 ......

Column name must be valid identifier - alphanumeric starting with latin letter, but you have a number 10010325 for column name - this won't work! 列名必须是有效的标识符 - 以10010325开头的字母数字,但列名称的编号为10010325 - 这不起作用!

UPDATE : 更新

Adding columns dynamically sounds like recipe for disaster. 动态添加列听起来像灾难的配方。 You will be better off by using proper database schema, like this: 通过使用适当的数据库模式,您将会更好,如下所示:

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

and then create attendance table like this: 然后创建这样的考勤表:

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

Table student_attendance is now supposed to have extra row for every student attending given class. student_attendance现在应该为每个参加课程的学生提供额外的行。 This way, you do not have to alter your schema on a fly. 这样,您就不必动态更改架构。

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

相关问题 我可以通过调用另一个类的方法动态地将新列添加到sqlite表吗? - can i dynamically add new column to sqlite table by calling a method from another class? 无法将数据添加到 android 中的 SQLite 表 - Unable to add data to SQLite table in android 无法从动态创建的sqlite表的列中检索单个字符串数据,并导致“请求索引0,大小为0” - Unable to retrieve a single string-data from sqlite table'column which is dynamically created and cause “Index 0 requested, with a size of 0” 如何在Sqlite的数据库表中添加额外的列? - How to add an additional column in a database table in Sqlite? 向已经存在的sqlite表添加唯一列 - Add unique column to already exist sqlite table Sqlite在某个位置将表添加到表中(Android) - Sqlite Add Column into table at a certain position (Android) 如何在 android SQLite 中的现有表上添加列? - How to add column on existing table in android SQLite? 如何动态地将表(sqlite)中的行添加到布局? (机器人) - How to dynamically add rows from a table (sqlite) to layout? (Android) 我们可以在 SQLite 数据库中添加一个表作为另一个表的列吗? - Can we add a table to as a column of another table in SQLite Database? 向Android SQLite数据库添加新列创建新表 - Add new Column to Android SQLite Database Create new table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM