· 8 years ago · Feb 18, 2018, 04:26 AM
1import java.io.FileOutputStream;
2import java.io.IOException;
3import java.io.InputStream;
4import java.io.OutputStream;
5import java.util.Locale;
6import android.content.ContentValues;
7import android.content.Context;
8import android.database.Cursor;
9import android.database.SQLException;
10import android.database.sqlite.SQLiteDatabase;
11import android.database.sqlite.SQLiteException;
12import android.database.sqlite.SQLiteOpenHelper;
13import android.database.sqlite.SQLiteQueryBuilder;
14import android.util.Log;
15
16public class SqlHelper extends SQLiteOpenHelper {
17public static final String DATABASE_PATH = "/data/data/apppackage-name/databases/";
18public static final String DATABASE_NAME = "profiledatabase.db";
19private static final int DATABASE_VERSION = 1;
20
21public static final String PROFILES_TABLE = "profiles";
22public static final String COLUMN_PROFILE_ID = "profile_id";
23public static final String COLUMN_NAME = "name";
24public static final String COLUMN_BIRTHDAY = "birthday";
25public static final String COLUMN_EMAIL = "email";
26public static final String COLUMN_GENDER = "gender";
27
28public static final String INTERESTS_TABLE = "interests";
29public static final String COLUMN_ID = "_id";
30public static final String COLUMN_TITLE = "title";
31public static final String COLUMN_SELECTED = "selected";
32
33public static final String PROFILESINTERESTS_TABLE = "profiles_interests";
34private static long profile_id = -1;
35
36private static final String CREATE_TABLE_1 =
37 " create table " + PROFILES_TABLE +
38 " (profile_id integer primary key autoincrement," +
39 " name text not null, birthday date not null, email text not null, gender text not null);";
40
41private static final String CREATE_TABLE_2 =
42 " create table " + INTERESTS_TABLE +
43 " (_id integer primary key autoincrement," +
44 " title text not null, selected integer);";
45
46private static final String CREATE_TABLE_3 =
47 " create table " + PROFILESINTERESTS_TABLE +
48 " (profile_id integer primary key," +
49 " _id integer);";
50
51
52
53public SQLiteDatabase dbSqlite;
54private boolean dbExist = false;
55private final Context myContext;
56
57public SqlHelper(Context context) {
58 super(context, DATABASE_NAME, null, DATABASE_VERSION);
59 this.myContext = context;
60}
61
62/*@Override
63public void onCreate(SQLiteDatabase db) {
64 // check if exists and copy database from resource
65 createDB();
66}*/
67
68@Override
69public void onCreate(SQLiteDatabase db) {
70createDB();
71db.execSQL(CREATE_TABLE_1);
72db.execSQL(CREATE_TABLE_2);
73db.execSQL(CREATE_TABLE_3);
74}
75@Override
76public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
77 Log.w("SqlHelper", "Upgrading database from version " + oldVersion
78 + " to " + newVersion + ", which will destroy all old data");
79 onCreate(db);
80}
81
82public void createDatabase() {
83 createDB();
84}
85
86private void createDB() {
87 dbExist = DBExists();
88 if (!dbExist) {
89 copyDBFromResource();
90 }
91}
92
93public boolean dbExisting() {
94 return dbExist;
95}
96
97private boolean DBExists() {
98 SQLiteDatabase db = null;
99 try {
100 String databasePath = DATABASE_PATH + DATABASE_NAME;
101 db = SQLiteDatabase.openDatabase(databasePath, null,
102 SQLiteDatabase.OPEN_READWRITE);
103 db.setLocale(Locale.getDefault());
104 db.setLockingEnabled(true);
105 db.setVersion(1);
106 } catch (SQLiteException e) {
107 Log.e("SqlHelper", "database not found");
108 }
109 if (db != null) {
110 db.close();
111 }
112 return db != null ? true : false;
113}
114
115private void copyDBFromResource() {
116 InputStream inputStream = null;
117 OutputStream outStream = null;
118 String dbFilePath = DATABASE_PATH + DATABASE_NAME;
119 try {
120 inputStream = myContext.getAssets().open(DATABASE_NAME);
121 outStream = new FileOutputStream(dbFilePath);
122 byte[] buffer = new byte[1024];
123 int length;
124 while ((length = inputStream.read(buffer)) > 0) {
125 outStream.write(buffer, 0, length);
126 }
127 outStream.flush();
128 outStream.close();
129 inputStream.close();
130 } catch (IOException e) {
131 throw new Error("Problem copying database from resource file.");
132 }
133}
134
135public void openDataBase() throws SQLException {
136 String myPath = DATABASE_PATH + DATABASE_NAME;
137 dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
138 SQLiteDatabase.OPEN_READWRITE);
139}
140
141@Override
142public synchronized void close() {
143 if (dbSqlite != null)
144 dbSqlite.close();
145 super.close();
146}
147
148public void addProfiles( String name, String birthday, String email, String gender) {
149 ContentValues values = new ContentValues();
150 values.put("name", name);
151 values.put("birthday", birthday);
152 values.put("email", email);
153 values.put("gender", gender);
154 profile_id = dbSqlite.insert(PROFILES_TABLE, null, values);
155 }
156
157 public void addProfilesInterests( String iid) {
158 ContentValues values = new ContentValues();
159 values.put(COLUMN_PROFILE_ID, profile_id);
160 values.put(COLUMN_ID, iid);
161 profile_id = dbSqlite.insert(PROFILESINTERESTS_TABLE, null, values);
162 }
163
164 public Cursor getProfiles(){
165 return dbSqlite.query(PROFILES_TABLE, new String[] {
166 "name", " birthday", "email", "gender"}, null, null, null, null, null);
167 }
168
169 public Cursor getCursor() {
170 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
171 queryBuilder.setTables(INTERESTS_TABLE);
172 String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_SELECTED };
173 Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,null, null, null, "title ASC");
174 return mCursor;
175}
176 public void clearSelections() {
177 ContentValues values = new ContentValues();
178 values.put("selected", 0);
179 this.dbSqlite.update(SqlHelper.INTERESTS_TABLE, values, null, null);
180 }
181
182}
183
184contextWrapper.openOrCreateDatabase(sqlDBName, MODE_PRIVATE, null);
185sqLiteDatabase = SQLiteDatabase.openDatabase(path, factory, flags);
186sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(file, factory);
187
188File f = getDatabasePath(DataBaseAccess.DATABASE_NAME);
189String dbPath = f.getAbsolutePath();
190createPathIfNotExist(dbPath);
191int lastChar = path.lastIndexOf(File.separatorChar);
192String dir = path.substring(0, lastChar);
193File d = new File(dir);
194d.mkdirs();
195
196File dbFile = getApplicationContext().getDatabasePath("any.db");
197if (!dbFile.exists()) {
198 new File(dbFile.getParent()).mkdirs();
199 try {
200 InputStream is = getApplicationContext().getAssets().open("any.db");
201 OutputStream os = new FileOutputStream(dbFile);
202
203 byte[] buffer = new byte[1024];
204 while (is.read(buffer) > 0) {
205 os.write(buffer);
206 }
207 os.flush();
208
209 os.close();
210 is.close();
211 } catch (IOException e) {
212 e.printStackTrace();
213 }
214}