2015-10-15 35 views
0

我已經找到了這個主題的幾個答案(例如this),但它不起作用。我只收到警告,它不能解決方法'openOrCreateDatabase(java.lang.String,int,null)'。如何在android中使用SQLite?

這裏是我的源代碼:

public class DBHandler 
{ 
    SQLiteDatabase database; 
    DBHandler() 
    { 
     database = openOrCreateDatabase("DatabaseName", Context.MODE_PRIVATE, null); 
    } 
} 
+4

試過[官例](http://developer.android.com/guide/topics/data /data-storage.html#db)? –

+1

試試這個:http://www.vogella.com/tutorials/AndroidSQLite/article.html – Tobi

回答

6

SQLite是存儲數據到設備上的文本文件中的開源SQL數據庫。 Android內置了SQLite數據庫實現。

請檢查下面的鏈接

  1. Android SQLite Database Tutorial

  2. SQLite Database Tutorial

  3. SQLite and Android

結構

public class MySQLiteHelper extends SQLiteOpenHelper { 

    public static final String TABLE_COMMENTS = "comments"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_COMMENT = "comment"; 

    private static final String DATABASE_NAME = "commments.db"; 
    private static final int DATABASE_VERSION = 1; 

    // Database creation sql statement 
    private static final String DATABASE_CREATE = "create table " 
     + TABLE_COMMENTS + "(" + COLUMN_ID 
     + " integer primary key autoincrement, " + COLUMN_COMMENT 
     + " text not null);"; 

    public MySQLiteHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w(MySQLiteHelper.class.getName(), 
     "Upgrading database from version " + oldVersion + " to " 
      + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS); 
    onCreate(db); 
    } 

} 
+0

http://learnzone.info/android-database-example-beginners/ –

1
public class Sqlhelper extends SQLiteOpenHelper { 
private SQLiteDatabase db; 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_FNAME = "firstname"; 

Sqlhelper DB = null; 
private static final String DATABASE_NAME = "dbname.db"; 
private static final int DATABASE_VERSION = 2; 
public static final String DATABASE_TABLE_NAME = "db"; 

private static final String DATABASE_TABLE_CREATE = 
     "CREATE TABLE " + DATABASE_TABLE_NAME + "(" + 
       "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+ 
       "firstname TEXT NOT NULL);"; 



public Sqlhelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    try{ 

     db.execSQL(DATABASE_TABLE_CREATE); 
     Log.d("DATABASE", "Table Was Created"); 


    }catch(Exception e){ 
     e.printStackTrace(); 
    } 


} 
public void open() { 

    getWritableDatabase(); 
} 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    onCreate(db); 
    Log.d("DATABASE", "Table Was UPDATED"); 

} 
+2

好極了,這將有助於他理解Android中的SQL數據庫如何工作...... -1 – Tobi

+0

謝謝..... @ Tobi – Karthick

2

正如評論者給你的例子,你需要創建SQLiteOpenHelper類的子類,並重寫的onCreate和和onUpgrade方法,這將創建數據庫和表。然後,您可以使用此助手類的方法getReadableDatabase()getWritableDatabase()獲取SQLite數據庫的副本。您可以對此對象執行查詢。 下面的代碼片段演示了它。

public class DBAdapter { 
private SQLiteDatabase database; 
private Context context; 
private DatabaseHelper dbHelper; 


private class DatabaseHelper extends SQLiteOpenHelper { 

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
     super(context, name, factory, version); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_TABLES_QUERY); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXIST "+TABLE_QUERY); 
    } 
} 

public DBAdapter(Context ctx) { 
    this.context = ctx; 

} 

public DBAdapter open() throws SQLException { 
    dbHelper = new DatabaseHelper(context, DBNAME, null,DBVERSION); 
    database = dbHelper.getWritableDatabase(); 
    return this; 
} 

public Cursor executeQuery() { 
    Cursor result = database.rawQuery(YOUR_QUERY, null); 
    return result; 
} 

}

使用SQLite Open Helper developers guide更多的幫助。

我希望它有幫助。

0

使用下面的例子來創建數據庫

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

     import com.ayconsultancy.sumeshmedicals.SumeshMedicalContext; 
     import com.ayconsultancy.sumeshmedicals.model.PlaceModel; 
     import com.ayconsultancy.sumeshmedicals.utils.Utils; 

     import org.json.JSONArray; 
     import org.json.JSONException; 
     import org.json.JSONObject; 

     import java.util.ArrayList; 

     /** 
     * Created by Admin33 on 16-02-2016. 
     */ 
     public class DBHelper extends SQLiteOpenHelper { 
      static String DATABASE_NAME = "sumesh_medicals"; 
      static int DATABASE_VERSION = 1; 
      static DBHelper dbHelperInstance; 
      static SQLiteDatabase db; 

      public DBHelper(Context context) { 
       super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      } 

      public static DBHelper getInstance() { 
       if (dbHelperInstance == null) { 
        dbHelperInstance = new DBHelper(SumeshMedicalContext.getContext()); 
       } 
       return dbHelperInstance; 
      } 

      @Override 
      public void onCreate(SQLiteDatabase db) { 
       Utils.ShowLogD("in sqlite oncreate"); 
       try { 
        db.execSQL(DBQueries.CREATE_OTC_TABLE); 
        db.execSQL(DBQueries.CREATE_SHOP_DETAILS_TABLE); 
        db.execSQL(DBQueries.CREATE_USER_DETAILS_TABLE); 
        db.execSQL(DBQueries.CREATE_CITY_TABLE); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       // insertIntoShopDetails(db); 
       // insertIntoOTcPrescrion(db); 

      } 

      public synchronized SQLiteDatabase getDababase() { 
       if (db == null || (db != null && !db.isOpen())) { 
        db = this.getWritableDatabase(); 
       } 
       return db; 
      } 

      public synchronized void close() { 
       super.close(); 
       if (db != null) 
        db.close(); 
      } 

      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

      } 
} 
0

保存在主

public class MainActivity extends AppCompatActivity { 

String one,two; 
EditText name,phone; 
Button saveButton; 
List<StudentModel> list = new ArrayList<StudentModel>(); 
DatabaseHelper db; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    db = new DatabaseHelper(getApplicationContext()); 

    saveButton=(Button)findViewById(R.id.submit); 
    name=(EditText)findViewById(R.id.textName); 
    phone=(EditText)findViewById(R.id.textPhone); 

    saveButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      StudentModel student = new StudentModel(); 
      student.name = name.getText().toString(); 
      student.phone_number = phone.getText().toString(); 
      db.addStudentDetail(student); 

      list = db.getAllStudentsList(); 
      print(list); 




     } 
    }); 
} 
private void print(List<StudentModel> list) { 

    String value = ""; 
    for(StudentModel sm : list){ 
     value = value+"id: "+sm.id+", name: "+sm.name+" Ph_no: "+sm.phone_number+"\n"; 
    } 

    Log.i("<<<<<<<<<<",value); 

} 


} 

然後創建處理程序類

public class DatabaseHelper extends SQLiteOpenHelper { 

// Database Name 
public static String DATABASE_NAME = "student_database"; 


private static final int DATABASE_VERSION = 1; 


private static final String TABLE_STUDENTS = "students"; 


private static final String KEY_ID = "id"; 
private static final String KEY_NAME = "name"; 
private static final String KEY_PHONENUMBER = "phone_number"; 

public static String TAG = "tag"; 



private static final String CREATE_TABLE_STUDENTS = "CREATE TABLE " 
     + TABLE_STUDENTS + "(" + KEY_ID 
     + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," 
     + KEY_PHONENUMBER + " TEXT);"; 

public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 

    db.execSQL(CREATE_TABLE_STUDENTS); 

} 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_STUDENTS); 

    onCreate(db); 
} 


public long addStudentDetail(StudentModel student) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    // Creating content values 
    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, student.name); 
    values.put(KEY_PHONENUMBER, student.phone_number); 

    // insert row in students table 

    long insert = db.insert(TABLE_STUDENTS, null, values); 

    return insert; 
} 


public int updateEntry(StudentModel student) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    // Creating content values 
    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, student.name); 
    values.put(KEY_PHONENUMBER, student.phone_number); 

    return db.update(TABLE_STUDENTS, values, KEY_ID + " = ?", 
      new String[] { String.valueOf(student.id) }); 
} 


public void deleteEntry(long id) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_STUDENTS, KEY_ID + " = ?", 
      new String[] { String.valueOf(id) }); 
} 

public StudentModel getStudent(long id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 


    String selectQuery = "SELECT * FROM " + TABLE_STUDENTS + " WHERE " 
      + KEY_ID + " = " + id; 
    Log.d(TAG, selectQuery); 

    Cursor c = db.rawQuery(selectQuery, null); 

    if (c != null) 
     c.moveToFirst(); 

    StudentModel students = new StudentModel(); 
    students.id = c.getInt(c.getColumnIndex(KEY_ID)); 
    students.phone_number = c.getString(c.getColumnIndex(KEY_PHONENUMBER)); 
    students.name = c.getString(c.getColumnIndex(KEY_NAME)); 

    return students; 
} 


public List<StudentModel> getAllStudentsList() { 
    List<StudentModel> studentsArrayList = new ArrayList<StudentModel>(); 

    String selectQuery = "SELECT * FROM " + TABLE_STUDENTS; 
    Log.d(TAG, selectQuery); 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor c = db.rawQuery(selectQuery, null); 


    if (c.moveToFirst()) { 
     do { 

      StudentModel students = new StudentModel(); 
      students.id = c.getInt(c.getColumnIndex(KEY_ID)); 
      students.phone_number = c.getString(c 
        .getColumnIndex(KEY_PHONENUMBER)); 
      students.name = c.getString(c.getColumnIndex(KEY_NAME)); 


      studentsArrayList.add(students); 
     } while (c.moveToNext()); 
    } 

    return studentsArrayList; 
} 
} 

然後設置和獲取值第一以下數據sqlite的使用

public class StudentModel { 

public int id; 
public String name; 
public String phone_number; 

public StudentModel(int id, String name, String phone_number) { 
    // TODO Auto-generated constructor stub 
    this.id = id; 
    this.name = name; 
    this.phone_number = phone_number; 
} 
public StudentModel(){ 

} 
} 

嘗試

0

這裏是我的代碼如何使用SQLite數據庫

DatabaseHelper.java

  public class DatabaseHelper extends SQLiteOpenHelper { 

        public DatabaseHelper(Context context) { 
         super(context, "loginDB.db", null, 1); 
         getWritableDatabase(); 
        } 

        @Override 
        public void onCreate(SQLiteDatabase sqLiteDatabase) { 
         String query = "create table registration (id INTEGER,Image BLOB,firstname VARCHAR,Lastname VARCHAR,DateOfBirth VARCHAR,Phone VARCHAR,Gender VARCHAR, Email VARCHAR primary key,Password VARCHAR);"; 
         sqLiteDatabase.execSQL(query); 
        } 

        @Override 
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

        } 
      //method for insert data 
      public boolean insertRecord(byte[] imageInByte,String fn) { 
       SQLiteDatabase db = this.getWritableDatabase(); 
       ContentValues contentValues = new ContentValues(); 
       contentValues.put("Image", imageInByte); 
       contentValues.put("Firstname", fn); 
       db.insert("signup", null, contentValues); 
       return true; 
      } 

     public boolean updaterecord(String fn,String ln,String unme){ 
       SQLiteDatabase db = this.getWritableDatabase(); 
       ContentValues contentValues = new ContentValues(); 
       contentValues.put("Firstname", fn); 
       contentValues.put("Lastname", ln); 
       contentValues.put("Username", unme); 
       return db.update(TABLE_NAME, contentValues, "Username = ?", new String[]{(unme)}) > 0; 
      } 

public boolean deleterecord(String FirstName) { 

      SQLiteDatabase db = this.getWritableDatabase(); 
      return db.delete(TABLE_NAME, "Firstname = ?", new String[]{FirstName}) > 0; 
     } 

      public int displayDetail(String email, String password) { 
        SQLiteDatabase db = this.getWritableDatabase(); 
        Cursor cursor = db.rawQuery("select * from registration where Email='" + email + "'AND Password='" + password + "'", null); 

        return cursor.getCount(); 
       } 

       public ArrayList displayDetails(String email, String password) { 
        SQLiteDatabase db = this.getWritableDatabase(); 
        Cursor cursor = db.rawQuery("select * from registration where Email='" + email + "'AND Password='" + password + "'", null); 

        ArrayList<ModelClass> arrayList = new ArrayList(); 
        ModelClass model; 
        if (cursor != null) { 
         if (cursor.getCount() > 0) { 
          while (cursor.moveToNext()) { 
           model = new Model(); 
           model.setFirstname(cursor.getString(cursor.getColumnIndex("Firstname"))); 
           model.setLastname(cursor.getString(cursor.getColumnIndex("Lastname"))); 
           arrayList.add(model); 
          } 
         } 
        } 
        return arrayList; 
       } 
       } 
1

創建 「數據庫」 名包,包括它

創建SQLitHelper名稱等級

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
public class SQLitHelper extends SQLiteOpenHelper { 
    public static final String DataBase_Name = "ABC"; 
    public static final int Version = 1; 
    public static final String TblUser = "TblUser"; 
    public static final String TblClassList = "TblClassList"; 
    public static final String TblStudentList = "TblStudentList"; 

    public SQLitHelper(Context context) { 
     super(context, DataBase_Name, null, Version); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("Create table " + TblUser + 
       "(id INTEGER PRIMARY KEY," + 
       "uid INTEGER," + 
       "fname TEXT," + 
       "lname TEXT," + 
       "email TEXT," + 
       "password TEXT," + 
       "teacher TEXT," + 
       "student TEXT," + 
       "parent TEXT," + 
       "status TEXT," + 
       "landing_page TEXT," + 
       "createdate TEXT," + 
       "birthdate TEXT," + 
       "profilepic TEXT," + 
       "phone TEXT," + 
       "address TEXT," + 
       "gender TEXT," + 
       "age TEXT," + 
       "googleid TEXT," + 
       "facebookid TEXT," + 
       "alert_time TEXT," + 
       "sch_name TEXT,"+ 
       "login_with TEXT,"+ 
       "default_zone TEXT)"); 

     db.execSQL("Create table " + TblClassList + 
       "(id INTEGER PRIMARY KEY," + 
       "cid INTEGER," + 
       "uid INTEGER," + 
       "title TEXT," + 
       "color TEXT," + 
       "startdate TEXT," + 
       "enddate TEXT," + 
       "qrcode TEXT," + 
       "createdate TEXT," + 
       "not_submitted_count TEXT," + 
       "status TEXT," + 
       "extra1 TEXT," + 
       "extra2 TEXT)"); 

     db.execSQL("Create table " + TblStudentList + 
       "(id INTEGER PRIMARY KEY," + 
       "uid INTEGER," + 
       "cid INTEGER," + 
       "fname TEXT," + 
       "lname TEXT," + 
       "email TEXT," + 
       "profilepic TEXT," + 
       "student_name TEXT," + 
       "isleader TEXT," + 
       "add_homework TEXT," + 
       "track_submission TEXT," + 
       "status TEXT," + 
       "edit_homework TEXT," + 
       "del_homework TEXT," + 
       "last_access TEXT)"); 
    } 


    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    } 

} 

創建DataHelper類

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 
import java.util.ArrayList; 
import java.util.HashMap; 

public class DataHelper { 
    SQLitHelper sqLitHelper; 
    SQLiteDatabase sqLiteDatabase; 
    Context context; 
    final String TAG = "DataHelper"; 

    public DataHelper(Context context) { 
     sqLitHelper = new SQLitHelper(context); 
     this.context = context; 
     sqLiteDatabase = sqLitHelper.getWritableDatabase(); 
    } 


public void open() { 
     try { 
      sqLiteDatabase = sqLitHelper.getWritableDatabase(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void close() { 
     try { 
      sqLiteDatabase.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void insertUser(HashMap<String, String> list) { 
     ContentValues values = new ContentValues(); 
     open(); 
     try { 
      for (String str : list.keySet()) 
       values.put(str, list.get(str)); 
      long rowId = sqLiteDatabase.insert(SQLitHelper.TblUser, null, values); 
     } catch (Exception e) { 
      Log.e(TAG, "insertUser " + e.toString()); 
     } finally { 
      close(); 
     } 
    } 

    public void updateUser(HashMap<String, String> list, int uid) { 
     ContentValues values = new ContentValues(); 
     open(); 
     try { 
      for (String str : list.keySet()) 
       values.put(str, list.get(str)); 
      long rows = sqLiteDatabase.update(SQLitHelper.TblUser, values, "uid=" + uid, null); 
     } catch (Exception e) { 
      Log.e(TAG, "insertUser " + e.toString()); 
     } finally { 
      close(); 
     } 
    } 

    public int getUserRecordCount() { 
     int count = 0; 
     try { 
      open(); 
      Cursor cursor = sqLiteDatabase.rawQuery("Select * from " + SQLitHelper.TblUser, null); 
      count = cursor.getCount(); 
      cursor.close(); 
     } catch (Exception e) { 
      Logger.debugLog(TAG, "userCount : " + e.toString()); 
     } finally { 
      close(); 
     } 
     return count; 
    } 


    public HashMap<String,String> getUserDetail(){ 
     HashMap<String, String> list = new HashMap<>(); 
     Cursor cursor = null; 
     try { 
      open(); 
      cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + SQLitHelper.TblUser, null); 
      if (cursor.getColumnCount() > 0) { 
       while (cursor.moveToNext()) { 

        list.put("uid", cursor.getString(cursor.getColumnIndex("uid"))); 
        list.put("fname", cursor.getString(cursor.getColumnIndex("fname"))); 
        list.put("lname", cursor.getString(cursor.getColumnIndex("lname"))); 
        list.put("default_zone", cursor.getString(cursor.getColumnIndex("default_zone"))); 
        list.put("teacher", cursor.getString(cursor.getColumnIndex("teacher"))); 
        list.put("student", cursor.getString(cursor.getColumnIndex("student"))); 
        list.put("parent", cursor.getString(cursor.getColumnIndex("parent"))); 
        list.put("email", cursor.getString(cursor.getColumnIndex("email"))); 
        list.put("gender", cursor.getString(cursor.getColumnIndex("gender"))); 
        list.put("birthdate", cursor.getString(cursor.getColumnIndex("birthdate"))); 
        list.put("profilepic", cursor.getString(cursor.getColumnIndex("profilepic"))); 
        list.put("sch_name", cursor.getString(cursor.getColumnIndex("sch_name"))); 
        list.put("login_with", cursor.getString(cursor.getColumnIndex("login_with"))); 

       } 
      } 
     } catch (Exception e) { 
      Logger.debugLog(TAG, "getUserDetail : " + e.toString()); 
     } finally { 
      close(); 
      if (cursor != null) 
       if (!cursor.isClosed()) 
        cursor.close(); 
     } 
     return list; 
    } 

    public boolean deleteUserList() { 
     try { 
      open(); 
      if (sqLiteDatabase.delete(SQLitHelper.TblUser, null, null) > 0){ 
       return true; 
      }else { 
       return false; 
      } 
     } catch (Exception e) { 
      Logger.debugLog(TAG, "deleteUserList : " + e.toString()); 
     } finally { 
      close(); 
     } 
     return false; 
    } 


    public boolean insertClassList(MClassList mClassList) { 
     try { 
      open(); 
      ContentValues contentValues = new ContentValues(); 
      contentValues.put("cid", mClassList.getId()); 
      contentValues.put("uid", mClassList.getUid()); 
      contentValues.put("title", mClassList.getTitle()); 
      contentValues.put("color", mClassList.getColor()); 
      contentValues.put("startdate", mClassList.getStartdate()); 
      contentValues.put("enddate", mClassList.getEnddate()); 
      contentValues.put("qrcode", mClassList.getQrcode()); 
      contentValues.put("createdate", mClassList.getCreatedate()); 
      contentValues.put("status", mClassList.getStatus()); 
      contentValues.put("not_submitted_count", mClassList.getNot_sub_count()); 
      long id = sqLiteDatabase.insert(SQLitHelper.TblClassList, null, contentValues); 
      Logger.debugLog(TAG, "insertClassList : Sus"); 
      return true; 
     } catch (Exception e) { 
      Logger.debugLog(TAG, "insertClassList : " + e.toString()); 
     } finally { 
      close(); 
     } 
     return false; 
    } 

    public ArrayList<MClassList> getClassList() { 
     ArrayList<MClassList> clssArrayList = new ArrayList<>(); 
     Cursor cursor = null; 
     try { 
      open(); 
      String Query = QueryBuilder.classListQuery(); 
      cursor = sqLiteDatabase.rawQuery(Query, null); 
      if (cursor.getColumnCount() > 0) { 
       while (cursor.moveToNext()) { 
        MClassList mClassList = new MClassList(); 

        mClassList.setId(cursor.getInt(cursor.getColumnIndex("cid"))); 
        mClassList.setUid(cursor.getInt(cursor.getColumnIndex("uid"))); 
        mClassList.setTitle(cursor.getString(cursor.getColumnIndex("title"))); 
        mClassList.setColor(cursor.getString(cursor.getColumnIndex("color"))); 
        mClassList.setStartdate(cursor.getString(cursor.getColumnIndex("startdate"))); 
        mClassList.setEnddate(cursor.getString(cursor.getColumnIndex("enddate"))); 
        mClassList.setQrcode(cursor.getString(cursor.getColumnIndex("qrcode"))); 
        mClassList.setCreatedate(cursor.getString(cursor.getColumnIndex("createdate"))); 
        mClassList.setStatus(cursor.getString(cursor.getColumnIndex("status"))); 
        mClassList.setNot_sub_count(cursor.getString(cursor.getColumnIndex("not_submitted_count"))); 

        clssArrayList.add(mClassList); 
       } 
      } 
     } catch (Exception e) { 
      Logger.debugLog(TAG, "getClassList : " + e.toString()); 
     } finally { 
      close(); 
      if (cursor != null) 
       if (!cursor.isClosed()) 
        cursor.close(); 
     } 
     return clssArrayList; 
    } 

    public boolean deleteClassList() { 
     try { 
      open(); 
      if (sqLiteDatabase.delete(SQLitHelper.TblClassList, null, null) > 0){ 
       return true; 
      }else { 
       return false; 
      } 

     } catch (Exception e) { 
      Logger.debugLog(TAG, "deleteClassList : " + e.toString()); 
     } finally { 
      close(); 
     } 
     return false; 
    } 

    public boolean deleteStudentList() { 
     try { 
      open(); 
      if (sqLiteDatabase.delete(SQLitHelper.TblStudentList, null, null) > 0){ 
       return true; 
      } 
      else{ 
       return false; 
      } 

     } catch (Exception e) { 
      Logger.debugLog(TAG, "deleteStudentList : " + e.toString()); 
     } finally { 
      close(); 
     } 
     return false; 
    } 

    public void deleteStudent(int cid,int uid) { 
     try { 
      open(); 
      sqLiteDatabase.delete(SQLitHelper.TblStudentList, "uid=" + uid + " AND cid=" + cid, null); 
     } catch (Exception e) { 
      Logger.debugLog(TAG, "deleteStudent : " + e.toString()); 
     } finally { 
      close(); 
     } 
    } 

} 

創建類的QueryBuilder

public class QueryBuilder { 

    public static String teacherABCList(int cid) { 
     Calendar c = Calendar.getInstance(); 
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
     String formatDate = df.format(c.getTime()).toString(); 

     String Query = "SELECT * FROM " + SQLitHelper.TblTeacherHomeworkAll + " WHERE cid='" + cid + "'" + " AND duedate>= " +"'"+ formatDate+"'" + " ORDER BY duedate DESC "; 
     return Query; 
    } 

============================== 

    public static String studentXXXListQuery(int uid,String status) { 
     Calendar c = Calendar.getInstance(); 
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
     String formatDate = df.format(c.getTime()).toString(); 


     String Query = "SELECT * FROM " + SQLitHelper.TblStudentHomeworkAll + " WHERE uid='" + uid + "'" + " AND status= " +"'"+ status+"'"+" AND isDone='N'" + " ORDER BY duedate DESC "; 
     return Query; 
    } 

=========================================== 

public static String studentListQuery(String questionID) { 
     String query = "SELECT * FROM " + SQLitHelper.TblStudentCheckAnswer + " WHERE qid=" + questionID; 
     return query; 
    } 

} 
相關問題