-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBHandler.java
More file actions
107 lines (96 loc) · 3.41 KB
/
DBHandler.java
File metadata and controls
107 lines (96 loc) · 3.41 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.littlehouse_design.qpstoday;
/**
* Created by johnkonderla on 11/21/16.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.Settings;
public class DBHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "QPSToday";
// Ingredients table name
private static final String TABLE_ADDRESSES = "addresses";
private static final String IP_ID = "ipID";
private static final String IP_ADDRESS = "ipAddress";
private static final String PORT = "port";
private String[] selectingNum = {"1"};
public DBHandler(MainActivity context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ADDRESS_TABLE = "CREATE TABLE " + TABLE_ADDRESSES + "(" +
IP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + IP_ADDRESS + " TEXT, " +
PORT + " INTEGER DEFAULT 0)";
db.execSQL(CREATE_ADDRESS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ADDRESSES);
onCreate(db);
}
public void dropDB() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE " + TABLE_ADDRESSES);
}
public void addIP(String IP, int port) {
SQLiteDatabase dbWrite = this.getWritableDatabase();
SQLiteDatabase dbRead = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + TABLE_ADDRESSES;
Cursor cursor = dbRead.rawQuery(countQuery, null);
ContentValues values = new ContentValues();
values.put(IP_ADDRESS, IP);
values.put(PORT, port);
if(cursor.getCount() < 1) {
dbWrite.insert(TABLE_ADDRESSES, null, values);
System.out.println("It was an insert");
} else {
dbWrite.update(TABLE_ADDRESSES, values, IP_ID + " = ?", selectingNum);
System.out.println("it was an update");
}
cursor.close();
dbWrite.close();
dbRead.close();
}
public String getIpAddress() {
SQLiteDatabase dbRead = this.getReadableDatabase();
String ipAddress;
String query = "SELECT * FROM " + TABLE_ADDRESSES;
Cursor cursor = dbRead.rawQuery(query, null);
if(cursor.moveToFirst()) {
if (cursor.getCount() < 1) {
ipAddress = "Not Set";
} else {
ipAddress = cursor.getString(1);
}
}
else {
ipAddress = "Not Set";
}
cursor.close();
dbRead.close();
return ipAddress;
}
public int getPort() {
SQLiteDatabase dbRead = this.getReadableDatabase();
int port;
String query = "SELECT * FROM " + TABLE_ADDRESSES;
Cursor cursor = dbRead.rawQuery(query, null);
if(cursor.moveToFirst()) {
if (cursor.getCount() < 1) {
port = -1;
} else {
port = Integer.parseInt(cursor.getString(2));
}
} else {
port = -1;
}
cursor.close();
return port;
}
}