2012-09-28 113 views
-1

我試圖在我的應用程序中存儲解析的數據,但即使我已經初始化該類,數據庫也不會創建。這裏是我的代碼:未創建數據庫

public class Schedule extends ListActivity{ 
    // Flag for Internet connection status 
    Boolean isInternetPresent = false; 

    // Alert Dialog Manager 
    AlertDialogManager alert = new AlertDialogManager(); 

    ConnectionDetector cd; 

    // All static variables 
    static final String URL = "url address"; 

    // XML node keys 
    static final String KEY_ITEM = "group_header"; // Parent node 
    static final String KEY_NAME = "name"; //Group Number 
    static final String KEY_SUNM = "day_sun_mrg"; 
    static final String KEY_SUNE = "day_sun_evn"; 
    static final String KEY_MONM = "day_mon_mrg"; 
    static final String KEY_MONE = "day_mon_evn"; 
    static final String KEY_TUEM = "day_tue_mrg"; 
    static final String KEY_TUEE = "day_tue_evn"; 
    static final String KEY_WEDM = "day_wed_mrg"; 
    static final String KEY_WEDE = "day_wed_evn"; 
    static final String KEY_THUM = "day_thu_mrg"; 
    static final String KEY_THUE = "day_thu_evn"; 
    static final String KEY_FRIM = "day_fri_mrg"; 
    static final String KEY_FRIE = "day_fri_evn"; 
    static final String KEY_SATM = "day_sat_mrg"; 
    static final String KEY_SATE = "day_sat_evn"; 

    public static ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 

    // Creating new HashMap 
    HashMap<String, String> map = new HashMap<String, String>(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //Calling SQlite Process 
     DBAdapter dbc=new DBAdapter(this); //Here I am doing my class call 

     XMLParser parser = new XMLParser(); 
     String xml = parser.getXmlFromUrl(URL); // Getting XML 
     Document doc = parser.getDomElement(xml); // Getting DOM element 

     cd = new ConnectionDetector(getApplicationContext()); 

     // Check if Internet present 
     isInternetPresent = cd.isOnline(); 
     if (!isInternetPresent) { 
      // Internet Connection is not present 
      alert.showAlertDialog(Schedule.this, "Internet Connection Error", 
        "Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 
     else { 
      NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
      // Looping through all item nodes <item> 
      for (int i = 0; i < nl.getLength(); i++) { 
       Element e = (Element) nl.item(i); 
       // Adding each child node to HashMap key => value 

       map.put(KEY_NAME, parser.getValue(e,KEY_NAME)); 
       map.put(KEY_SUNM,parser.getValue(e, KEY_SUNM)); 
       map.put(KEY_SUNE, parser.getValue(e,KEY_SUNE)); 
       map.put(KEY_MONM,parser.getValue(e, KEY_MONM)); 
       map.put(KEY_MONE, parser.getValue(e,KEY_MONE)); 
       map.put(KEY_TUEM,parser.getValue(e, KEY_TUEM)); 
       map.put(KEY_TUEE, parser.getValue(e,KEY_TUEE)); 
       map.put(KEY_WEDM,parser.getValue(e, KEY_WEDM)); 
       map.put(KEY_WEDE, parser.getValue(e,KEY_WEDE)); 
       map.put(KEY_THUM,parser.getValue(e, KEY_THUM)); 
       map.put(KEY_THUE, parser.getValue(e,KEY_THUE)); 
       map.put(KEY_FRIM,parser.getValue(e, KEY_FRIM)); 
       map.put(KEY_FRIE, parser.getValue(e,KEY_FRIE)); 
       map.put(KEY_SATM,parser.getValue(e, KEY_SATM)); 
       map.put(KEY_SATE, parser.getValue(e,KEY_SATE)); 

       // Adding HashList to ArrayList 
       menuItems.add(map); 
      } 
     } 
    } 
} 

這是我DatabaseActivity類

public class DBAdapter{ 
    // Contacts Table Columns names 
    public static final String KEY_ROWID = "_id"; 
    private static final String KEY_NAME = "group_id"; 
    private static final String KEY_SUNM = "day_sun_mrg"; 
    private static final String KEY_SUNE = "day_sun_evn"; 
    private static final String KEY_MONM = "day_mon_mrg"; 
    private static final String KEY_MONE = "day_mon_evn"; 
    private static final String KEY_TUEM = "day_tue_mrg"; 
    private static final String KEY_TUEE = "day_tue_evn"; 
    private static final String KEY_WEDM = "day_wed_mrg"; 
    private static final String KEY_WEDE = "day_wed_evn"; 
    private static final String KEY_THUM = "day_thu_mrg"; 
    private static final String KEY_THUE = "day_thu_evn"; 
    private static final String KEY_FRIM = "day_fri_mrg"; 
    private static final String KEY_FRIE = "day_fri_evn"; 
    private static final String KEY_SATM = "day_sat_mrg"; 
    private static final String KEY_SATE = "day_sat_evn"; 

    private static final String TAG = "DBAdapter"; 

    // Database Name 
    private static final String DATABASE_NAME = "name"; 

    // Contacts table name 
    private static final String DATABASE_TABLE= "schedule"; 


    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE ="create table schedules (_id integer primary key autoincrement,group_id text,day_sun_mrg text ,day_sun_evn text ,day_mon_mrg text,day_mon_evn text ,day_tue_mrg text,day_tue_evn text,day_wed_mrg text ,day_wed_evn text ,day_thu_mrg text,day_thu_evn text ,day_fri_mrg text ,day_fri_evn text ,day_sat_mrg text,day_sat_evn text)"; 


    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 


    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     private static final String TAG = null; 

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

     // Creating Tables 

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

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

    //---Opens the database--- 
    public DBAdapter open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---closes the database--- 
    public void close() 
    { 
     DBHelper.close(); 
    } 


    public void insertintoDatabase(String sunm) { 
     // TODO Auto-generated method stub 
     ContentValues values = new ContentValues(); 

     values.put(KEY_SUNM, sunm); //Sunday Morning 

     db.insert(DATABASE_TABLE, null, values); 
    } 


    //---Deletes a particular title--- 
    public boolean deleteTitle(long group) 
    { 
     return db.delete(DATABASE_TABLE, KEY_NAME + 
       "=" + group, null) > 0; 
    } 

    //---Retrieves all the titles--- 
    public Cursor getAllTitles() 
    { 
     return db.query(DATABASE_TABLE, new String[] { 
       KEY_NAME, 
        KEY_SUNE, 
        KEY_SUNM, 
        KEY_SUNE,KEY_MONM,KEY_MONE,KEY_TUEM, 
        KEY_TUEE,KEY_WEDM,KEY_WEDE,KEY_THUM, 
        KEY_THUE,KEY_FRIM,KEY_FRIE,KEY_SATM,KEY_SATE}, 
       null, 
       null, 
       null, 
       null, 
       null); 
    } 

    //---Retrieves a particular title--- 
    public Cursor getTitle(long group) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_TABLE, new String[] { 
         KEY_NAME, 
          KEY_SUNE, 
          KEY_SUNM, 
          KEY_SUNE,KEY_MONM,KEY_MONE,KEY_TUEM, 
          KEY_TUEE,KEY_WEDM,KEY_WEDE,KEY_THUM, 
          KEY_THUE,KEY_FRIM,KEY_FRIE,KEY_SATM,KEY_SATE 
         }, 
         KEY_NAME + "=" + group, 
         null, 
         null, 
         null, 
         null, 
         null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
     } 

    //---Updates a title--- 
    public boolean updateTitle(long group, String sunm, String sune, String monm, 
       String mone, String tuem, String tuee, String wedm, String wede, 
       String thum, String thue, String frim, String frie, String satm, 
       String sate) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_NAME,group); // Group number 
     args.put(KEY_SUNM, sunm); // Sunday morning 
     args.put(KEY_SUNM, sune); // Sunday evening 
     args.put(KEY_MONM, monm); 
     args.put(KEY_MONE, mone); 
     args.put(KEY_TUEM, tuem); 
     args.put(KEY_TUEE, tuem); 
     args.put(KEY_WEDM, wedm); 
     args.put(KEY_WEDE, wede); 
     args.put(KEY_THUM, thum); 
     args.put(KEY_THUE, thue); 
     args.put(KEY_FRIM, frim); 
     args.put(KEY_FRIE, frie); 
     args.put(KEY_SATM, satm); 
     args.put(KEY_SATE, sate); 
     return db.update(DATABASE_TABLE, args, 
          KEY_NAME + "=" + group, null) > 0; 
    } 


    public long insertData(String sunm, String sune, String monm, String mone, 
      String tuem, String tuee, String wedm, String wede, String thum, 
      String thue, String frim, String frie, String satm, String sate, 
      String string) { 
     // TODO Auto-generated method stub 

     ContentValues initialValues = new ContentValues(); 
     //initialValues.put(KEY_NAME,group); // Group number 
     initialValues.put(KEY_SUNM, sunm); //Sunday morning 
     initialValues.put(KEY_SUNM, sune);// Sunday evening 
     initialValues.put(KEY_MONM, monm); 
     initialValues.put(KEY_MONE, mone); 
     initialValues.put(KEY_TUEM, tuem); 
     initialValues.put(KEY_TUEE, tuem); 
     initialValues.put(KEY_WEDM, wedm); 
     initialValues.put(KEY_WEDE, wede); 
     initialValues.put(KEY_THUM, thum); 
     initialValues.put(KEY_THUE, thue); 
     initialValues.put(KEY_FRIM, frim); 
     initialValues.put(KEY_FRIE, frie); 
     initialValues.put(KEY_SATM, satm); 
     initialValues.put(KEY_SATE, sate); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 
} 
+0

您是否收到任何錯誤?如果是這樣請附上日誌貓 –

+0

不存在任何logcat錯誤 – surhidamatya

回答

0

感謝@Marcin幫助我,但我找到了答案,而試圖。我做了:

DBAdapter dba; 
dba = new DBAdapter(this); 
try { 
    dba.open(); 
} catch (SQLException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

它幫了我。我不知道它是否會幫助你。

+1

@Marcin S.感謝幫助哥們 – surhidamatya

2

你缺少在創建語句的結束分號。因此它永遠不會被執行和創建。看看這個:

DATABASE_CREATE ="create table schedules (_id integer primary key autoincrement,group_id text,day_sun_mrg text ,day_sun_evn text ,day_mon_mrg text,day_mon_evn text ,day_tue_mrg text,day_tue_evn text,day_wed_mrg text ,day_wed_evn text ,day_thu_mrg text,day_thu_evn text ,day_fri_mrg text ,day_fri_evn text ,day_sat_mrg text,day_sat_evn text)"; 

您需要添加一個分號,;,最後括號後。將其更改爲:

DATABASE_CREATE ="create table schedules (_id integer primary key autoincrement,group_id text,day_sun_mrg text ,day_sun_evn text ,day_mon_mrg text,day_mon_evn text ,day_tue_mrg text,day_tue_evn text,day_wed_mrg text ,day_wed_evn text ,day_thu_mrg text,day_thu_evn text ,day_fri_mrg text ,day_fri_evn text ,day_sat_mrg text,day_sat_evn text);"; 
+0

最後括號後面有分號 – surhidamatya

+0

請看看我的編輯。你需要一個結束sql命令 –

+0

yepp我做了它,但沒有做任何更改(沒有創建數據庫) – surhidamatya