如何存储和连接sqlite数据库?

时间:2015-03-25 12:36:37

标签: android database sqlite

我正在研究android项目。它就像一个测验。我提供多项选择答案的问题,因此如果答案错误,用户只能选择一个答案将转到简单的YouTube视频或仅提供链接。 所以问题是:sqlite数据库是否完美? 以及我将如何存储它们?并将其与应用程序连接? 我看到了一些代码,但没有提到活动代码。 我不善于用android开发,所以我很困惑, PLZ帮助我。一个月后我项目的截止日期。 提前致谢 。

1 个答案:

答案 0 :(得分:1)

首先,你要创建你的dbhelper类

    import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {

private final static String DB_NAME = "miacassa.db";
private final static int DB_VERSION = 4;

public DbHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(EntrateTableHelper.CREATE_QUERY);
    db.execSQL(UsciteTableHelper.CREATE_QUERY);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE " + EntrateTableHelper.TABLE_NAME);
    db.execSQL(EntrateTableHelper.CREATE_QUERY);
    db.execSQL("DROP TABLE " + UsciteTableHelper.TABLE_NAME);
    db.execSQL(UsciteTableHelper.CREATE_QUERY);

    }

}

然后,你为你的每张桌子创建一个tablehelper

import android.provider.BaseColumns;

public class EntrateTableHelper implements BaseColumns {

public static final String TABLE_NAME = "entrate";
public static final String ENTRATA = "entrata";
public static final String DATA = "data";
public static final String DESCRIZIONE = "descrizione";
public static final String ORA = "ora";

public static String CREATE_QUERY = "CREATE TABLE " + TABLE_NAME + " ("
        + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ENTRATA
        + " TEXT NOT NULL, " + DATA
        + " TEXT NOT NULL, "+ DESCRIZIONE
        + " TEXT NOT NULL, "+ ORA
        + " TEXT NOT NULL "+ " );";
}


import android.provider.BaseColumns;

public class UsciteTableHelper implements BaseColumns {

public static final String TABLE_NAME = "uscite";
public static final String USCITE = "uscite";
public static final String DATA = "data";
public static final String DESCRIZIONE = "descrizione";
public static final String ORA = "ora";

public static String CREATE_QUERY = "CREATE TABLE " + TABLE_NAME + " ("
        + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + USCITE
        + " TEXT NOT NULL, " + DATA
        + " TEXT NOT NULL, "+ DESCRIZIONE
        + " TEXT NOT NULL, "+ ORA
        + " TEXT NOT NULL "+ " );";
}

比你创建内容提供商

 import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import com.giurgica.manageerasmusmoney.database.EntrateTableHelper;
import com.giurgica.manageerasmusmoney.database.UsciteTableHelper;;

公共类MyContentProvider扩展了ContentProvider {

public static final String AUTHORITY = "com.giurgica.manageerasmusmoney.database";

public static final String ENTRATE_PATH = "entrate";
public static final String USCITE_PATH = "uscite";

public static final Uri ENTRATE_URI = Uri.parse(ContentResolver.SCHEME_CONTENT + "://" + AUTHORITY + "/" + ENTRATE_PATH);
public static final Uri USCITE_URI = Uri.parse(ContentResolver.SCHEME_CONTENT + "://" + AUTHORITY + "/" + USCITE_PATH);



private static final int FULL_ENTRATE_TABLE = 0;
private static final int SINGLE_ENTRATE = 100;
private static final int FULL_USCITE_TABLE = 1;
private static final int SINGLE_USCITA = 101;


private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

static{
    URI_MATCHER.addURI(AUTHORITY, ENTRATE_PATH, FULL_ENTRATE_TABLE); 
    URI_MATCHER.addURI(AUTHORITY, ENTRATE_PATH + "/#", SINGLE_ENTRATE);
    URI_MATCHER.addURI(AUTHORITY, USCITE_PATH, FULL_USCITE_TABLE); 
    URI_MATCHER.addURI(AUTHORITY, USCITE_PATH + "/#", SINGLE_USCITA);
}

private DbHelper helper;

@Override
public boolean onCreate() {

    helper = new DbHelper(getContext());
    return true;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {

    SQLiteDatabase database = helper.getWritableDatabase();
    int result = 0;
    String tmp;
    switch (URI_MATCHER.match(uri)) {
    case FULL_ENTRATE_TABLE:
        result = database.delete(EntrateTableHelper.TABLE_NAME, selection,
                selectionArgs);
        break;
    case SINGLE_ENTRATE:
        tmp = EntrateTableHelper._ID + " = "
                + uri.getLastPathSegment();
        result = database.delete(EntrateTableHelper.TABLE_NAME,
                selection + "AND" + tmp, selectionArgs);
        break;
    case FULL_USCITE_TABLE:
        result = database.delete(UsciteTableHelper.TABLE_NAME, selection,
                selectionArgs);
        break;
    case SINGLE_USCITA:
         tmp = UsciteTableHelper._ID + " = "
                + uri.getLastPathSegment();
        result = database.delete(UsciteTableHelper.TABLE_NAME,
                selection + "AND" + tmp, selectionArgs);
        break;
    default:
        break;
    }

    if (result != 0)
        getContext().getContentResolver().notifyChange(uri, null);

    return result;
}

public static final String MIME_TYPE_SFALCI = ContentResolver.CURSOR_DIR_BASE_TYPE
        + "/sfalci";

public static final String MIME_TYPE_SFALCIO = ContentResolver.CURSOR_ITEM_BASE_TYPE
        + "/sfalcio";

@Override
public String getType(Uri uri) {

    switch (URI_MATCHER.match(uri)) {
    case FULL_ENTRATE_TABLE:
        return MIME_TYPE_SFALCI;

    case SINGLE_ENTRATE:
        return MIME_TYPE_SFALCIO;

    default:
        break;
    }

    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {

    if (URI_MATCHER.match(uri) == FULL_ENTRATE_TABLE) {
        SQLiteDatabase database = helper.getWritableDatabase();
        long result = database.insert(EntrateTableHelper.TABLE_NAME, null,
                values);

        getContext().getContentResolver().notifyChange(uri, null);

        return Uri.parse(ENTRATE_URI + "/" + result);

    }
    if (URI_MATCHER.match(uri) == FULL_USCITE_TABLE) {
        SQLiteDatabase database = helper.getWritableDatabase();
        long result = database.insert(UsciteTableHelper.TABLE_NAME, null,
                values);

        getContext().getContentResolver().notifyChange(uri, null);

        return Uri.parse(USCITE_URI + "/" + result);

    }
    return null;
}



@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    switch (URI_MATCHER.match(uri)) {
        case FULL_ENTRATE_TABLE:
            queryBuilder.setTables(EntrateTableHelper.TABLE_NAME);
            break;
        case SINGLE_ENTRATE:
            queryBuilder.setTables(EntrateTableHelper.TABLE_NAME);
            queryBuilder.appendWhere(EntrateTableHelper._ID + "="
                    + uri.getLastPathSegment());
            break;
        case FULL_USCITE_TABLE:
            queryBuilder.setTables(UsciteTableHelper.TABLE_NAME);
            break;
        case SINGLE_USCITA:
            queryBuilder.setTables(UsciteTableHelper.TABLE_NAME);
            queryBuilder.appendWhere(UsciteTableHelper._ID + "="
                    + uri.getLastPathSegment());
            break;
        default:
            break;
    }
    SQLiteDatabase vdDatabase = helper.getReadableDatabase();

    Cursor cursor = queryBuilder.query(vdDatabase, projection, selection,
            selectionArgs, null, null, "_ID DESC");

    cursor.setNotificationUri(getContext().getContentResolver(), ENTRATE_URI);

    return cursor;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {

    SQLiteDatabase database = helper.getWritableDatabase();
    int result = 0;
    String tmp;

    switch (URI_MATCHER.match(uri)) {
    case FULL_ENTRATE_TABLE:
        result = database.update(EntrateTableHelper.TABLE_NAME, values,
                selection, selectionArgs);

        break;
    case SINGLE_ENTRATE:
        tmp = EntrateTableHelper._ID + " = " + uri.getLastPathSegment();
        result = database.update(EntrateTableHelper.TABLE_NAME, values,
                selection + "AND" + tmp, selectionArgs);

        break;
    case FULL_USCITE_TABLE:
        result = database.update(UsciteTableHelper.TABLE_NAME, values,
                selection, selectionArgs);

        break;
    case SINGLE_USCITA:
        tmp = UsciteTableHelper._ID + " = " + uri.getLastPathSegment();
        result = database.update(UsciteTableHelper.TABLE_NAME, values,
                selection + "AND" + tmp, selectionArgs);
        break;
    default:
        break;
    }

    if(result!=0)
        getContext().getContentResolver().notifyChange(uri, null);

    return result;
}

}

最后,您可以在活动中使用数据库

private SQLiteDatabase database;
DbHelper helper = new DbHelper(this);
database = helper.getWritableDatabase();
Cursor cursor = database.query(EntrateTableHelper.TABLE_NAME, new String[] {
            EntrateTableHelper.ENTRATA}, null, null,
            null, null, null);

    int entrate = cursor.getColumnIndex(EntrateTableHelper.ENTRATA);
String soldi;
while(cursor.moveToNext()){
         soldi = cursor.getString(entrate);


    }

    cursor.close();