2013-06-04 5 views
0

J'ai créé des classes simples pour la base de données, mais lorsque je l'ai testé sur un périphérique réel, l'application s'est arrêtée!La base de données Sqlite ne peut pas être créée

public class MySQLiteHelper extends SQLiteOpenHelper 
{ 
    public static final String TABLE_NAME = "caffe"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_ARTIKAL = "artikal"; 
    public static final String COLUMN_PRICE = "price"; 
    public static final String COLUMN_AMOUNT = "amount"; 
    //public static final String COLUMN_TIME = "date&time"; 


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


    private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + COLUMN_ID+ " integer primary key autoincrement," +COLUMN_ARTIKAL + " text not null"+ COLUMN_PRICE + " real not null"+ COLUMN_AMOUNT+" integer 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_NAME); 
     onCreate(db); 
    } 

classe DataAccessObject:

public class BD_DAO 
{ 
    private SQLiteDatabase database; 
    private MySQLiteHelper dbHelper; 
    private String[] allColumns = {MySQLiteHelper.COLUMN_ARTIKAL,MySQLiteHelper.COLUMN_PRICE, MySQLiteHelper.COLUMN_AMOUNT}; 

    public BD_DAO(Context context) 
    { 
     dbHelper = new MySQLiteHelper(context); 
    } 

    public void open() throws SQLException 
    { 
     database = dbHelper.getWritableDatabase(); 
    } 

    public void close() 
    { 
     dbHelper.close(); 
    } 

    public void createItem(Item item) 
    { 
     ContentValues values = new ContentValues(); 
     values.put(MySQLiteHelper.COLUMN_ARTIKAL, item.getArtikal().toString()); 
     values.put(MySQLiteHelper.COLUMN_PRICE, Double.toString(item.getPrice()).toString()); 
     values.put(MySQLiteHelper.COLUMN_AMOUNT, Integer.toString(item.getAmount()).toString()); 
     database.insert(MySQLiteHelper.TABLE_NAME, null,values); 
    } 

    public List<Item> getAllItems() 
    { 
     List<Item> items = new ArrayList<Item>(); 
     Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME, allColumns, null, null, null, null, null); 
     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) 
     { 
      Item cursorItem = cursorToItem(cursor); 
      items.add(cursorItem); 
      cursor.moveToNext(); 
     } 
     cursor.close(); 
     return items; 
    } 

    private Item cursorToItem(Cursor cursor) 
    { 
     Item item = new Item(); 
     item.setArtikal(cursor.getString(1)); 
     item.setPrice(cursor.getDouble(2)); 
     item.setAmount(cursor.getInt(3)); 
     return item; 
    } 
} 

Et classe principale:

......listview, some buttons...... 
Button DB=(Button)findViewById(R.id.DB); 
DB.setOnClickListener(new View.OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     databasesource.open(); 
     /*for (Item s:tableList) 
     { 
      databasesource.createItem(s);}*/ 
      databasesource.close(); 
      adapter.clear(); 
    } 
}); 

application fonctionne très bien jusqu'à ce que j'appuyez sur le bouton, et puis juste crier vers le bas. D'abord j'ai essayé d'envoyer des données en db, l'application s'est arrêtée, puis j'ai essayé de créer simplement db, mais j'ai encore arrêté! Dois-je faire quelques changements est-il manifeste d'android ou un autre .xml?

Merci d'avance !!

Comment mettre logcat?

+2

Pourriez-vous s'il vous plaît montrer votre LogCat? – koesie10

+0

mettez votre logcat .. – Riser

+0

pourquoi ne pas avoir placé "," après ce '+ COLUMN_ARTIKAL +" texte non nul "' il devrait y avoir '+ COLUMN_ARTIKAL +" texte non nul, "' –

Répondre

1

J'ai fait quelques modifications mineures avec votre code. Vous pouvez utiliser le même pour référence.

Modifiez ci-dessous en fonction de vos besoins. J'ai eu un échantillon similaire et j'ai modifié la même chose avec votre code. Je ne suis pas sûr si c'est le meilleur moyen. Mais vous pouvez utiliser le ci-dessous et modifier le même en fonction de vos besoins.

dans votre MainActivity pour ajouter

Button DB=(Button)findViewById(R.id.DB); 
DB.setOnClickListener(new View.OnClickListener() 
{ 
@Override 
public void onClick(View v) 
{ 
    Double price = Double.parseDouble(et2.getText().toString()); 
    int amount = Integer.parseInt(et3.getText().toString()); 
    MySQLiteHelper db = new MySQLiteHelper (MainActivity.this); 
    db.createItem(new Item(et1.getText().toString(),price,amount)); 
    db.close(); 
} 
}); 

MySQLiteHelper

public class MySQLiteHelper extends SQLiteOpenHelper { 

public static final String TABLE_NAME = "caffe"; 
// private SQLiteDatabase database; 
// private MySQLiteHelper dbHelper; 
private String[] allColumns = {MySQLiteHelper.COLUMN_ARTIKAL,MySQLiteHelper.COLUMN_PRICE, MySQLiteHelper.COLUMN_AMOUNT}; 

public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_ARTIKAL = "artikal"; 
public static final String COLUMN_PRICE = "price"; 
public static final String COLUMN_AMOUNT = "amount"; 
// public static final String COLUMN_TIME = "date&time"; 
Context context; 

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

// changed the below  
private static final String DATABASE_CREATE = "create table " + 
     TABLE_NAME + "(" + COLUMN_ID + 
     " integer primary key autoincrement," + COLUMN_ARTIKAL + 
     " text not null," + COLUMN_PRICE + " real not null," + 
     COLUMN_AMOUNT + " integer not null)"; 

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

@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_NAME); 
    onCreate(db); 
} 


    public void createItem(Item item) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(MySQLiteHelper.COLUMN_ARTIKAL, item.getArticle().toString()); 
     values.put(MySQLiteHelper.COLUMN_PRICE, Double.toString(item.getPrice()).toString()); 
     values.put(MySQLiteHelper.COLUMN_AMOUNT, Integer.toString(item.getAmount()).toString()); 
     //Toast.makeText(context, "Inserted", 1000).show(); 
     db.insert(MySQLiteHelper.TABLE_NAME, null,values); 

    } 

    public List<Item> getAllItems() { 

     List<Item> items = new ArrayList<Item>(); 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.query(MySQLiteHelper.TABLE_NAME, 
      allColumns, null, null, null, null, null); 

     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      Item cursorItem = cursorToItem(cursor); 
      items.add(cursorItem); 
      cursor.moveToNext(); 
     } 

     cursor.close(); 
     return items; 
    } 

    private Item cursorToItem(Cursor cursor) { 
     Item item = new Item(); 
     item.setArticle(cursor.getString(0)); 
     item.setPrice(cursor.getDouble(1)); 
     item.setAmount(cursor.getInt(2)); 

     return item; 
    } 
} 

classe article

public class Item { 

String article; 
Double price; 
int amount; 

public Item() 
{ 

} 
public Item(String article,Double price, int amount) 
{ 
    this.article=article; 
    this.price=price; 
    this.amount= amount; 
} 
public String getArticle() { 
    return article; 
} 

public void setArticle(String article) { 
    this.article = article; 
} 

public Double getPrice() { 
    return price; 
} 

public void setPrice(Double price) { 
    this.price = price; 
} 

public int getAmount() { 
    return amount; 
} 

public void setAmount(int amount) { 
    this.amount = amount; 
} 

}

SnapShot

enter image description here

Affichage sur le bouton du clic. Le toast affiche uniquement le nom de l'article.

 MySQLiteHelper db = new MySQLiteHelper (MainActivity.this); 
List<Item> contacts = db.getAllItems();  
StringBuilder sb = new StringBuilder(); 
for (Item cn : contacts) 
    { 
    sb.append(cn.getArticle()); 
    sb.append("\n");    
} 
Toast.makeText(MainActivity.this,sb, 1000).show(); 

enter image description here

+0

Merci! Je vous remercie! Je vous remercie!!!! – Mikky

2

essayez de cette façon.

DB.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    public void onClick(View v) { 

     BD_DAO db = new BD_DAO(getApplicationContext()); 
     //make your CRUD operation and then close database object 
     db.close(); 

    } 
}); 

Relire ce code avec votre code existant.

private static final String DATABASE_CREATE = "create table " + 
      TABLE_NAME + "(" + COLUMN_ID + 
      " integer primary key autoincrement," + COLUMN_ARTIKAL + 
      " text not null ," + COLUMN_PRICE + " real not null ," + 
      COLUMN_AMOUNT + " integer not null)"; 

Si vous trouvez un problème, alors mettez un commentaire.

+0

Non, éteignez-vous, encore une fois! – Mikky

+0

mettez votre logcat !! – Harshid

+0

Vous avez raison! Est-ce que logcat affiche une erreur uniquement lorsque j'essaie de tester sur un émulateur? – Mikky

1

Vous avez oublié de mettre une virgule remplacer ce code de votre

private static final String DATABASE_CREATE = "create table " + 
     TABLE_NAME + "(" + COLUMN_ID + 
     " integer primary key autoincrement," + COLUMN_ARTIKAL + 
     " text not null" + COLUMN_PRICE + " real not null" + 
     COLUMN_AMOUNT + " integer not null);"; 

à

private static final String DATABASE_CREATE = "create table " + 
     TABLE_NAME + "(" + COLUMN_ID + 
     " integer primary key autoincrement," + COLUMN_ARTIKAL + 
     " text not null," + COLUMN_PRICE + " real not null," + 
     COLUMN_AMOUNT + " integer not null)"; 
+0

Vous avez raison! Est-ce que logcat affiche une erreur uniquement lorsque j'essaie de tester sur un émulateur? – Mikky

+0

oui ............ – Saad

Questions connexes