Заготовка для работы с SQLite в Android

Маленький шаблон, для работы с SQLite:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class DBAdapter 
{
    private static final String DATABASE_NAME = "test";
    private static final int DATABASE_VERSION = 1;
 
    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
    {
        DatabaseHelper(Context context)
        {
            super(context,DATABASE_NAME, null, DATABASE_VERSION);
        }
 
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
 
        }
 
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
 
        }
    }
    public SQLiteDatabase GetDataBaseOpen() throws SQLException
    {
        return this.db;
    }
    public DBAdapter Open() throws SQLiteException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }
    public DBAdapter OpenForRead() throws SQLiteException
    {
        db = DBHelper.getReadableDatabase();
        return this;
    }
    public void Close() 
    {
        DBHelper.close();
    }
}
public class DBAdapter 
{
	private static final String DATABASE_NAME = "test";
	private static final int DATABASE_VERSION = 1;

	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
	{
		DatabaseHelper(Context context)
		{
			super(context,DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub

		}
	}
	public SQLiteDatabase GetDataBaseOpen() throws SQLException
	{
		return this.db;
	}
	public DBAdapter Open() throws SQLiteException
	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	public DBAdapter OpenForRead() throws SQLiteException
	{
		db = DBHelper.getReadableDatabase();
		return this;
	}
	public void Close() 
	{
		DBHelper.close();
	}
}

Добавить комментарий