Creating tables in sqlite database on android

前端 未结 5 1030
误落风尘
误落风尘 2020-12-17 02:28
@Override
        public void onCreate(SQLiteDatabase db)
        {
            try{
                db.execSQL(\"create table \" +          NotificationManager.getUserSt         


        
5条回答
  •  天命终不由人
    2020-12-17 02:58

                public class DBHandler extends SQLiteOpenHelper {
                    private static String DATABASE_NAME = "mydb";
                    private static int DATABASE_VERSION = 1;
    
                    String TABLE_NAME = "Student";
    
                    String KEY_ID = "id";
                    String KEY_STUDENT_NAME = "name";
                    String KEY_STUDENT_MARKS = "marks";
    
                public DBHandler(Context context) {
                        super(context, DATABASE_NAME, null, DATABASE_VERSION);
                    }
    
            @Override
                public void onCreate(SQLiteDatabase db) {
                    // creating table
                    String query = "CREATE TABLE " + TABLE_NAME +
                            " ( " +
                            KEY_ID + " INTEGER PRIMARY KEY," +
                            KEY_STUDENT_NAME + " TEXT," +
                            KEY_STUDENT_MARKS + " INTEGER"
                            " ) ";
                    db.execSQL(query);
    
                // insertion in the table
                ContentValues values = new ContentValues();
                values.put(KEY_QUESTION_ID, "1");
                values.put(KEY_STUDENT_NAME , "abc");
                values.put(KEY_STUDENT_MARKS , "95");
                db.insert(TABLE_NAME, null, values);
    
                values.put(KEY_QUESTION_ID, "2");
                values.put(KEY_STUDENT_NAME , "def");
                values.put(KEY_STUDENT_MARKS , "93");
                db.insert(TABLE_NAME, null, values);
        }
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub
    
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                // Create tables again
                onCreate(db);
            }
         // retrieve date
         List getAllData(int Id) {
    
                List student = new ArrayList();
                // Select All Query
                String selectQuery = "SELECT * FROM " + TABLE_NAME + "    WHERE KEY_ID =" + "'" + Id + "'";
    
                SQLiteDatabase db = this.getWritableDatabase();
    
                Cursor cursor = db.rawQuery(selectQuery, null);
    
                // looping through all rows and adding to list
                if (cursor.moveToFirst()) {
                    do {
                        Student std = new Student();
                        u.setId(cursor.getInt(0));
                        u.setStudentName(cursor.getString(1));
                        u.setMarks(cursor.getString(2));
    
                        student.add(std);
                    } while (cursor.moveToNext());
                }
                // return student list
                return student;
            }
        }
    
         public class Student {
    
            public static int Id;
            public static String studentName;
            public static int marks;
    
            public int getId() {
                return Id;
            }
    
            public static void setId(int id) {
                Id = id;
            }
    
            public String getStudentName() {
                return studentName;
            }
    
            public static void setStudentName(String studentName) {
                Student.studentName = studentName;
            }
    
            public int getMarks() {
                return marks;
            }
    
            public static void setMarks(int marks) {
                Student.marks = marks;
            }
        }
    
        public class MainActivity extends AppCompatActivity {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                DBHandler db = new DBHandler(getApplicationContext());
                List std = new ArrayList();
                rl = db.getAllData(1);
               for (Student s : std) {
                   Log.v("Id",s.getId());
                   Log.v("Name",s.getStudentName());
                   Log.v("Marks",s.getMarks());
           }
        }
    }
    

提交回复
热议问题