· 8 years ago · Aug 09, 2018, 09:02 AM
1SQLlite database is not getting created..Why?
2package com.lalsoft.janko;
3
4import android.content.ContentValues;
5import android.content.Context;
6import android.database.Cursor;
7import android.database.SQLException;
8import android.database.sqlite.SQLiteDatabase;
9import android.database.sqlite.SQLiteOpenHelper;
10import android.util.Log;
11
12public class DBAdapter
13{
14public static final String KEY_ROWID = "_id";
15public static final String KEY_GQTY = "gqty";
16public static final String KEY_NQTY = "nqty";
17public static final String KEY_DATE = "ddate";
18private static final String TAG = "DBAdapter";
19
20private static final String DATABASE_NAME = "lalaqua";
21private static final String DATABASE_TABLE = "nsales";
22private static final int DATABASE_VERSION = 1;
23
24private static final String DATABASE_CREATE =
25 "create table titles (_id integer primary key autoincrement, "
26 + "gqty text not null, nqty text not null, "
27 + "ddate text not null);";
28
29private final Context context;
30
31private DatabaseHelper DBHelper;
32private SQLiteDatabase db;
33
34public DBAdapter(Context ctx)
35{
36 this.context = ctx;
37 DBHelper = new DatabaseHelper(context);
38}
39
40private static class DatabaseHelper extends SQLiteOpenHelper
41{
42 DatabaseHelper(Context context)
43 {
44 super(context, DATABASE_NAME, null, DATABASE_VERSION);
45 }
46
47 @Override
48 public void onCreate(SQLiteDatabase db)
49 {
50 db.execSQL(DATABASE_CREATE);
51 }
52
53 @Override
54 public void onUpgrade(SQLiteDatabase db, int oldVersion,
55 int newVersion)
56 {
57 Log.w(TAG, "Upgrading database from version " + oldVersion
58 + " to "
59 + newVersion + ", which will destroy all old data");
60 db.execSQL("DROP TABLE IF EXISTS titles");
61 onCreate(db);
62 }
63}
64
65//---opens the database---
66public DBAdapter open() throws SQLException
67{
68 db = DBHelper.getWritableDatabase();
69 return this;
70}
71
72//---closes the database---
73public void close()
74{
75 DBHelper.close();
76}
77
78//---insert a title into the database---
79public long insertTitle(String gqty, String nqty, String ddate)
80{
81 ContentValues initialValues = new ContentValues();
82 initialValues.put(KEY_GQTY, gqty);
83 initialValues.put(KEY_NQTY, nqty);
84 initialValues.put(KEY_DATE, ddate);
85 return db.insert(DATABASE_TABLE, null, initialValues);
86}
87
88//---deletes a particular title---
89public boolean deleteTitle(long rowId)
90{
91 return db.delete(DATABASE_TABLE, KEY_ROWID +
92 "=" + rowId, null) > 0;
93}
94
95//---retrieves all the titles---
96public Cursor getAllTitles()
97{
98 return db.query(DATABASE_TABLE, new String[] {
99 KEY_ROWID,
100 KEY_GQTY,
101 KEY_NQTY,
102 KEY_DATE},
103 null,
104 null,
105 null,
106 null,
107 null);
108}
109
110//---retrieves a particular title---
111public Cursor getTitle(long rowId) throws SQLException
112{
113 Cursor mCursor =
114 db.query(true, DATABASE_TABLE, new String[] {
115 KEY_ROWID,
116 KEY_GQTY,
117 KEY_NQTY,
118 KEY_DATE
119 },
120 KEY_ROWID + "=" + rowId,
121 null,
122 null,
123 null,
124 null,
125 null);
126 if (mCursor != null) {
127 mCursor.moveToFirst();
128 }
129 return mCursor;
130}
131
132//---updates a title---
133public boolean updateTitle(long rowId, String gqtr,
134String nqty, String ddate)
135{
136 ContentValues args = new ContentValues();
137 args.put(KEY_GQTY, gqtr);
138 args.put(KEY_NQTY, nqty);
139 args.put(KEY_DATE, ddate);
140 return db.update(DATABASE_TABLE, args,
141 KEY_ROWID + "=" + rowId, null) > 0;
142}
143}
144
145package com.lalsoft.janko;
146
147import java.util.Calendar;
148import android.content.BroadcastReceiver;
149import android.content.Context;
150import android.content.Intent;
151import android.os.Bundle;
152import android.telephony.gsm.SmsManager;
153import android.telephony.gsm.SmsMessage;
154import android.util.Log;
155
156public class SMSReceiver extends BroadcastReceiver
157{
158public String SendMsgBody;
159private static final String LOG_TAG = "JankoSMS";
160public DBAdapter db;
161public Integer isDone=0;
162public Double GrossQty,NetQty;
163
164@Override
165public void onReceive(Context context, Intent intent)
166{
167 db = new DBAdapter(context);
168 //---get the SMS message passed in---
169 Bundle bundle = intent.getExtras();
170 SmsMessage[] msgs = null;
171 String str = "";
172 String PhNo;
173 String MsgBody;
174 if (bundle != null)
175 {
176 //---retrieve the SMS message received---
177 Object[] pdus = (Object[]) bundle.get("pdus");
178 msgs = new SmsMessage[pdus.length];
179 for (int i=0; i<msgs.length; i++){
180 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
181 str += "SMS from " + msgs[i].getOriginatingAddress();
182 PhNo=msgs[i].getOriginatingAddress();
183 str += " :";
184 str += msgs[i].getMessageBody().toString();
185 MsgBody=msgs[i].getMessageBody().toString();
186 str += "n";
187 // EncodeSMS(MsgBody);
188
189 String GQtyS,NQtyS,sDate;
190 GQtyS="Ok";
191 NQtyS="Done";
192 sDate=getDate();
193 Log.i(LOG_TAG, "Date" +" "+ sDate +" "+ GQtyS +" "+ NQtyS);
194 long id;
195 id = db.insertTitle(GQtyS ,NQtyS,sDate);
196
197 }
198
199 }
200}
201public static String getDate()
202{
203 Calendar c = Calendar.getInstance();
204
205 String sDate = c.get(Calendar.DAY_OF_MONTH) + "-"
206 + c.get(Calendar.MONTH)
207 + "-" + c.get(Calendar.YEAR);
208
209 //+ " at " + c.get(Calendar.HOUR_OF_DAY)
210 //+ ":" + c.get(Calendar.MINUTE);
211 return sDate;
212}
213}
214
215public long insertTitle(String gqty, String nqty, String ddate)
216{
217 open();
218 ContentValues initialValues = new ContentValues();
219 initialValues.put(KEY_GQTY, gqty);
220 initialValues.put(KEY_NQTY, nqty);
221 initialValues.put(KEY_DATE, ddate);
222 return db.insert(DATABASE_TABLE, null, initialValues);
223}
224
225public DBAdapter open() throws SQLException
226{
227 db = DBHelper.getWritableDatabase();
228 return this;
229}
230
231public void onCreate(SQLiteDatabase db) {
232System.out.println("table created....");
233 try{
234 db.execSQL(DATABASE_CREATE);
235 }catch (Exception e) {
236 // TODO: handle exception
237 }
238 }