اذهب إلى المحتوى

السؤال

نشر

بعد تنفيذ التطبيق والدخول إليه عند الضغط على button teacher  للتنقل إلى صفحة تسجيل الدخول الخاصة بال techers يخرج مباشرة ويأخذني إلى خطأ متعلق بقاعدة البيانات كما في الصورة في السطر 72  هل ممكن المساعدة؟

error

E/SQLiteLog: (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appmobile, PID: 30145
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appmobile/com.example.appmobile.teacher_page}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1 SQLITE_ERROR[1]): , while compiling: CREATE TABLE CLASS_TABLE(_CIDINTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,CLASS_NAME TEXT NOT NULL, SUBJECT_NAME TEXT NOT NULL, UNIQUE (CLASS_NAME,SUBJECT_NAME));
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3556)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7948)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
     
          Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1 SQLITE_ERROR[1]): , while compiling: CREATE TABLE CLASS_TABLE(_CIDINTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,CLASS_NAME TEXT NOT NULL, SUBJECT_NAME TEXT NOT NULL, UNIQUE (CLASS_NAME,SUBJECT_NAME));
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1372)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:811)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2321)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2249)
        at com.example.appmobile.DbHelper.onCreate(DbHelper.java:72)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:486)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:415)
        at com.example.appmobile.DbHelper.getClassTable(DbHelper.java:96)
        at com.example.appmobile.teacher_page.loadData(teacher_page.java:49)
        at com.example.appmobile.teacher_page.onCreate(teacher_page.java:37)
        at android.app.Activity.performCreate(Activity.java:7955)
        at android.app.Activity.performCreate(Activity.java:7944)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7948) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 

code

package com.example.appmobile;

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

import androidx.annotation.Nullable;

class DbHelper extends SQLiteOpenHelper {

    private static final int VERSION = 1;

    private static final String CLASS_TABLE_NAME = "CLASS_TABLE";
    public static final String C_ID = "_CID";
    public static final String CLASS_NAME_KEY = "CLASS_NAME";
    public static final String SUBJECT_NAME_KEY = "SUBJECT_NAME";

    private static final String CREATE_CLASS_TABLE =
            "CREATE TABLE " + CLASS_TABLE_NAME + "(" +
                    C_ID + "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                    CLASS_NAME_KEY + " TEXT NOT NULL, " +
                    SUBJECT_NAME_KEY + " TEXT NOT NULL, " +
                    "UNIQUE (" + CLASS_NAME_KEY + "," + SUBJECT_NAME_KEY + ")" +
                    ");";

    private static final String DROP_CLASS_TABLE = "DROP TABLE IF EXISTS "+CLASS_TABLE_NAME;
    private static final String SELECT_CLASS_TABLE = "SELECT * FROM "+CLASS_TABLE_NAME;

    private static final String STUDENT_TABLE_NAME = "STUDENT_TABLE";
    private static final String S_ID = "_SID";
    private static final String STUDENT_NAME_KEY = "STUDENT_NAME";
    private static final String STUDENT_ROLL_KEY = "ROLL";
    private static final String CREATE_STUDENT_TABLE =
    "CREATE TABLE " + STUDENT_TABLE_NAME +
            "( " +
            S_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
            C_ID + " INTEGER NOT NULL, "+
            STUDENT_NAME_KEY + " TEXT NOT NULL, " +
            STUDENT_ROLL_KEY + " INTEGER, "+
           " FOREIGN KEY ( "+C_ID+") REFERENCES "+ CLASS_TABLE_NAME + "("+C_ID+")"+
            ");";
    private static final String DROP_STUDENT_TABLE = "DROP TABLE IF EXISTS "+STUDENT_TABLE_NAME;
    private static final String SELECT_STUDENT_TABLE = "SELECT * FROM "+STUDENT_TABLE_NAME;


    private static final String STATUS_TABLE_NAME = "STATUS_TABLE";
    private static final String STATUS_ID = "_STATUS_CID";
    private static final String DATE_KEY = "STATUS_DATE";
    private static final String STATUS_KEY = "STATUS";

    private static final String CREATE_STATUS_TABLE =
            "CREATE TABLE " + STATUS_TABLE_NAME + "(" +
                    STATUS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "+
                    S_ID + " INTEGER NOT NULL, " +
                    DATE_KEY + " DATE NOT NULL, " +
                    STATUS_KEY + " TEXT NOT NULL, " +
                    "UNIQUE ("+ S_ID + "," + DATE_KEY+"),"+
                    " FOREIGN KEY ("+ S_ID+") REFERENCES "+ STUDENT_TABLE_NAME+"( "+S_ID+")"+
                    ");";

    private static final String DROP_STATUS_TABLE = "DROP TABLE IF EXISTS "+STATUS_TABLE_NAME;
    private static final String SELECT_STATUS_TABLE = "SELECT * FROM "+STATUS_TABLE_NAME;
    public DbHelper(@Nullable Context context) {
        super( context, "Attendance.db", null, VERSION  );
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_CLASS_TABLE);
    db.execSQL(CREATE_STUDENT_TABLE);
    db.execSQL(CREATE_STATUS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            db.execSQL( DROP_CLASS_TABLE );
            db.execSQL( DROP_STUDENT_TABLE );
            db.execSQL( DROP_STATUS_TABLE );
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    long addClass(String className,String subjectName){
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put( CLASS_NAME_KEY,className );
        values.put( SUBJECT_NAME_KEY,subjectName );

       return database.insert( CLASS_TABLE_NAME,null,values );
    }
    Cursor getClassTable(){
        SQLiteDatabase database = this.getReadableDatabase();

        return  database.rawQuery( SELECT_CLASS_TABLE,null );
    }
}

20220513_211720.jpg

20220513_211008.jpg

Appmobile2.rar

Appmobile2.rar

Recommended Posts

  • 0
نشر

المشكلة في محاولة تطبيق الزيادة التلقائية لحقل في الجدول قيمته ليست رقمية

AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

كما ظهر

while compiling: CREATE TABLE CLASS_TABLE(_CIDINTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
                                      ^^^^^^^^^^^

لاحظ أن اسم الحقل ملتصق مع نمطه و بذلك نتج قراءة خطأ، تأكد من وضع فراغ في

 C_ID + "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
        ^^^

 C_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
       ^^^^

 

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...