Sqlite 数据库无法创建 [英] Sqlite database can not be create

查看:41
本文介绍了Sqlite 数据库无法创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为数据库做了简单的类,但是当我在真机上测试时,应用程序关闭了!

I made simple classes for database, but when I test it on real device, application shut down!

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);
    }

类数据访问对象:

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;
    }
}

和主类:

......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();
    }
});

应用程序工作正常,直到我按下按钮,然后大喊大叫.首先我尝试在数据库中发送一些数据,应用程序关闭,然后我尝试创建数据库,但再次关闭!我应该做一些更改是 android manifest 还是其他一些 .xml?

Application works fine until I press button, and then just shout down. First I tried to send some data in db, application shut down, then I tried just to create db, but shut down again! Should I make some changes is android manifest or some other .xml?

提前致谢!!

如何放置logcat?

How to put logcat?

推荐答案

我对您的代码做了一些小改动.你可以参考一下.

I have made few minor changes with your code. You can use the same for reference.

根据您的要求修改以下内容.我有一个类似的示例,我用您的代码修改了相同的示例.我不确定这是否是最好的方法.但是您可以使用以下内容并根据您的要求进行修改.

Modify the below according to your requirements. I had a similar sample and i modified the same with your code. I am not sure if this is the best way. But you can use the below and modify the same according to your requirements.

在你的 MainActivity 中添加

In Your MainActivity to add

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

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;
    }
}

物品类别

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;
} 

}

截图

点击显示按钮时显示.Toast 仅显示文章名称.

Display on show button click. The toast displays only article name.

     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();

这篇关于Sqlite 数据库无法创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆