Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在android中创建一个多表SQL数据库大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为我的 Android应用程序创建一个多表数据库.我正在按照 http://androidforbeginners.blogspot.com/2010/01/creating-multiple-sqlite-database.html这个网站上的建议来做这件事.我继续得到以下错误.该错误似乎是由数据库表的onCreate引起的.

如果你在DBHelper类中查看我的onCreate,我有两个注释掉了.这使得它无论哪一个都不公开都可以工作.

必须有一种方法来创建多表数据库,因为数据库中的单个表几乎违背了拥有数据库的目的.

10-23 02:11:35.383: ERROR/AndroidRuntime(300): Caused by: android.database.sqlite.sqliteException: Can't upgrade read-only database from version 0 to 1: /data/data/com.parkingticket/databases/Tickets.db

提前致谢.

这是我的代码

package com.parkingticket;
   import java.sql.sqlException;
   import android.content.ContentValues;
   import android.content.Context;
   import android.database.cursor;
   import android.database.sqlite.sqliteDatabase;
   import android.database.sqlite.sqliteDatabase.cursorFactory;
   import android.database.sqlite.sqliteException;
   import android.database.sqlite.sqliteOpenHelper;
   import android.util.Log;

   public class TicketDBAdapter 
   {
private static final String DATABASE_NAME="Tickets.db";
private static final int DATABASE_VERSION = 1;

private static final String PARKEDCARS_TABLE = "ParkedCars";
private static final String PARKINGMETERS_TABLE = "ParkingMeters";
private static final String PARKINGTICKETS_TABLE = "ParkingTickets";
private static final String POLICEOFFICERS_TABLE = "PoliceOfficers";

// The name and column index for each column in PARKEDCARS
public static final String KEY_CARID = "carID";
    public static final int CARID_columN = 0;
public static final String KEY_CARMAKE = "Make";
    public static final int CARMAKE_columN = 1;
public static final String KEY_CARMODEL = "Model";
    public static final int CARMODEL_columN = 2;
public static final String KEY_CARCOLOR = "Color";
    public static final int CARCOLOR_columN = 3;
public static final String KEY_CARLICENSEnumbER = "Licensenumber";
    public static final int CARLICENSEnumbER_columN = 4;
public static final String KEY_CARminutESPARKED = "minutesParked";
    public static final int CARminutESPARKED_columN = 5;

// The name and column index for each column in PARKINGMETERS
public static final String KEY_METERID = "meterID";
    public static final int METERID_columN = 0;
public static final String KEY_minutESPURCHASED = "minutesPurchased";
    public static final int minutESPURCHASED_columN = 1;

// The name and column index for each column in PARKINGTICKETS
    //TODO create the columns and indexs for parking tickets

// The name and column index for each column in POLICEOFFICERS
public static final String KEY_OFFICERID = "officerID";
    public static final int OFFICERID_columN = 0;
public static final String KEY_OFFICERNAME = "Name";
    public static final int OFFICERNAME_columN = 1;
public static final String KEY_OFFICERBADGE = "Badgenumber";
    public static final int OFFICERBADE_columN = 2;


//Variable to hold the database instance
private sqliteDatabase ticketDB;

//Context of the application using the database.
private final Context context;

//Database open/upgrade Helper
private TicketDBHelper ticketDBHelper;

public TicketDBAdapter(Context _context)
{
    context = _context;
    ticketDBHelper = new TicketDBHelper(context,DATABASE_NAME,null,DATABASE_VERSION);
}

public void open() throws sqliteException
{
    try
    {
        ticketDB = ticketDBHelper.getWritableDatabase();
    }
    catch(sqliteException eX)
    {
        ticketDB = ticketDBHelper.getReadableDatabase();
    }
}

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

 //Insert a new ParkedCar
public long insertParkedCar(ParkedCar _car)
{
    //Create a new row of values to insert
    ContentValues newParkedCarValues = new ContentValues();

    //Assign values for each row
    newParkedCarValues.put(KEY_CARMAKE,_car.getMake());
    newParkedCarValues.put(KEY_CARMODEL,_car.getModel());
    newParkedCarValues.put(KEY_CARCOLOR,_car.getColor());
    newParkedCarValues.put(KEY_CARLICENSEnumbER,_car.getLicensenumber());
    newParkedCarValues.put(KEY_CARminutESPARKED,_car.getminutesParked());

    //Insert the row
    return ticketDB.insert(PARKEDCARS_TABLE,newParkedCarValues);
}

//Remove a ParkedCar based on its index
public Boolean removeParkedCar(long _rowIndeX)
{
    return ticketDB.delete(PARKEDCARS_TABLE,KEY_CARID + "=" + _rowIndex,null)>0;
}

//update a ParkedCar's minutesParked
//TODO Create an update for ParkedCar's minutesParked.

public cursor getAllParkedCarscursor()
{
    return ticketDB.query(PARKEDCARS_TABLE,new String[] {KEY_CARID,KEY_CARMAKE,KEY_CARMODEL,KEY_CARCOLOR,KEY_CARLICENSEnumbER,KEY_CARminutESPARKED},null);
}

public cursor setcursorParkedCar(long _rowIndeX) throws sqlException
{
    cursor result = ticketDB.query(true,PARKEDCARS_TABLE,new String []{KEY_CARID},null);

    if ((result.getCount() == 0) || !result.moveToFirst())
    {
        throw new sqlException("No ParkedCar found for row: " + _rowIndeX);
    }

    return result;
}

public static class TicketDBHelper extends sqliteOpenHelper
{
    public TicketDBHelper(Context context,String name,cursorFactory factory,int version)
    {
        super(context,name,factory,version);
    }

    //sql Statement to create PARKEDCARS table
    private static final String PARKEDCARS_CREATE = "create table " + PARKEDCARS_TABLE + " (" + KEY_CARID + " INTEGER PRIMary key autoincrement," + KEY_CARMAKE + " text not null," + KEY_CARMODEL + " text not null," + KEY_CARCOLOR + " text not null," + KEY_CARLICENSEnumbER + " text not null," + KEY_CARminutESPARKED + "int not null);";

    //sql Statement to create ParkingMeters table
    private static final String PARKINGMETERS_CREATE = "create table" + PARKINGMETERS_TABLE + " (" + KEY_METERID + " INTEGER PRIMary key autoincrement," + KEY_minutESPURCHASED + " int not null);";

    //sql Statement to create ParkingTickets table
    //TODO create the statement for parkingTickets

    //sql Statement to create PoliceOfficers table
    private static final String POLICEOFFICERS_CREATE = "create table" + POLICEOFFICERS_TABLE + " (" + KEY_OFFICERID + " INTEGER PRIMary key autoincrement," + KEY_OFFICERNAME + " text not null," + KEY_OFFICERBADGE + "text not null);";

    //Called when no database exists in disk and the Helper class needs to create a new one.
    @Override
    public void onCreate(sqliteDatabase _db)
    {
                //_db.execsql(PARKEDCARS_create);
          _db.execsql(PARKINGMETERS_create);
                 //_db.execsql(POLICEOFFICERS_create);
    }

    //Called when there is a database verion mismatch meaning that the version of the database on disk needs to be upgraded to the current version
    @Override
    public void onUpgrade(sqliteDatabase _db,int _oldVersion,int _newVersion)
    {
        //Log the version upgrade.
        Log.w("TaskDBAdapter","Upgrading from version " + _oldVersion + " to " + _newVersion + ",which will destroy all old data");

        //Upgrade the exisTing database to conform to the new version
        //Multiple prevIoUs versions can be handled by comparing _oldVersoin and _newVersion values

        //The simplest case is to drop teh old table and create a new one.
        _db.execsql("drop table IF EXISTS " + PARKEDCARS_TABLE);
        _db.execsql("drop table IF EXISTS " + PARKINGMETERS_TABLE);
        _db.execsql("drop table IF EXISTS " + POLICEOFFICERS_TABLE);

        onCreate(_db);
    }
}
  }

解决方法

我知道我迟到了,但我认为这可能有助于其他人阅读这篇文章.
我有同样的问题,它是关于空格的,当你在编写关于空间的SQL查询时要非常小心.例如在上面的代码中,你可以注意到最后没有空格:

 KEY_OFFICERBADGE“text not null”

所以基本上,你已经声明了一个没有类型的列.希望它有所帮助.

大佬总结

以上是大佬教程为你收集整理的在android中创建一个多表SQL数据库全部内容,希望文章能够帮你解决在android中创建一个多表SQL数据库所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。