This repository was archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathDatabaseHelper.java
81 lines (68 loc) · 2.65 KB
/
DatabaseHelper.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package nyc.c4q;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.sql.SQLException;
import java.util.List;
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String MYDB = "mydb.db";
private static final int VERSION = 1;
private static DatabaseHelper mHelper;
private DatabaseHelper getInstance(Context context) {
if (mHelper == null) {
mHelper = new DatabaseHelper(context.getApplicationContext());
return mHelper;
}
return mHelper;
}
public DatabaseHelper(Context context) {
super(context, MYDB, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Books.class);
TableUtils.createTable(connectionSource, Members.class);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Books.class, true);
TableUtils.dropTable(connectionSource, Members.class, true);
onCreate(database, connectionSource);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void insertMemberRow(int id, String name, int dob_month, int dob_day, int dob_year, String city, String state) {
Members member = new Members( name, dob_month, dob_day, dob_year, city, state);
try {
getDao(Members.class).create(member);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertBookRow(int id, String title, String author, String isbn, String isbn13, String publisher, int publishyear) {
Books book = new Books( title, author, isbn, isbn13, publisher, publishyear);
try {
getDao(Books.class).create(book);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Books> loadBookData() throws SQLException {
List<Books> isbn = getDao(Books.class).queryForAll();
return isbn;
}
public List<Members> loadMemberData() throws SQLException {
List<Members> name = getDao(Members.class).queryForAll();
return name;
}
}