· 8 years ago · Jul 28, 2018, 03:34 PM
1Error on Android's rawQuery()
2package com.math.scan;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.Scanner;
7
8import android.content.Context;
9import android.content.res.AssetManager;
10import android.database.sqlite.SQLiteDatabase;
11import android.database.sqlite.SQLiteOpenHelper;
12import android.util.Log;
13
14public class DbHelper extends SQLiteOpenHelper{
15
16 public static final String TABLE_NAME = "formulas";
17
18 public static final String COLUMN_ID = "_id";
19 public static final String COLUMN_UNKNOWN = "unknown";
20 public static final String COLUMN_FORMULA = "formula";
21 public static final String COLUMN_CTG = "ctg";
22
23 private static final String DB_NAME = "formulas.db";
24 private static int DB_VERSION = 1;
25
26 private static final String DB_CREATE = "CREATE TABLE "+TABLE_NAME+
27 "("+COLUMN_ID+" integer primary key autoincrement,"
28 +COLUMN_UNKNOWN+" text not null,"
29 +COLUMN_FORMULA+" text not null,"
30 +COLUMN_CTG+" text not null );";
31
32 public Context mCtx;
33
34
35 public DbHelper(Context context) {
36 super(context, DB_NAME, null, DB_VERSION);
37 this.mCtx = context;
38 }
39
40 @Override
41 public void onCreate(SQLiteDatabase database) {
42
43 database.execSQL(DB_CREATE);
44
45 // reading formulas
46 AssetManager asset = mCtx.getAssets();
47 try {
48 InputStream input = asset.open("formulas");
49
50 int size = input.available();
51
52 byte[] buffer = new byte[size];
53 input.read(buffer);
54 input.close();
55
56 String content = new String(buffer);
57
58 Scanner scan = new Scanner(content);
59
60 String current;
61 String[] f = new String[2];
62 String[] formula = new String[2];
63
64 while(scan.hasNextLine()) {
65 current = scan.nextLine();
66 // get category
67 f = current.split("!!");
68 formula = f[1].split(" = ");
69 database.execSQL("INSERT INTO `formulas` VALUES (NULL, '"+formula[0]+"', '"+formula[1]+"', '"+f[0]+"');");
70 Log.d("DB", "INSERT INTO `formulas` VALUES (NULL, '"+formula[0]+"', '"+formula[1]+"', '"+f[0]+"');");
71 }
72
73 } catch (IOException e) {
74 e.printStackTrace();
75 }
76 }
77
78 @Override
79 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
80 Log.w(DbHelper.class.getName(),
81 "Upgrading database from version " + oldVersion + " to "
82 + newVersion + ", which will destroy all old data");
83 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
84 onCreate(db);
85 }
86}
87
88package com.math.scan;
89
90import android.content.Context;
91import android.database.Cursor;
92import android.database.SQLException;
93import android.database.sqlite.SQLiteDatabase;
94import android.util.Log;
95
96public class FormulaModel {
97 private SQLiteDatabase database;
98 private DbHelper dbHelper;
99
100 public FormulaModel(Context context) {
101 dbHelper = new DbHelper(context);
102 }
103
104 public void open() throws SQLException {
105 dbHelper.getWritableDatabase();
106 }
107
108 public void close() {
109 dbHelper.close();
110 }
111
112 public void setFormulaList(String ctg) {
113 open();
114 // app stops working here
115 Cursor cursor = database.rawQuery("SELECT unknown, formula FROM formulas WHERE ctg = '"+ctg+"'", null);
116 int results = cursor.getCount();
117 cursor.moveToFirst();
118
119 Global.FormuleResult = new String[results];
120 Global.FormuleTable = new String[results];
121
122 for(int i = 0; i < results; i++) {
123 Global.FormuleTable[i] = cursor.getString(1);
124 Global.FormuleResult[i] = cursor.getString(0);
125 }
126 close();
127 }
128}
129
130package com.math.scan;
131
132import net.sourceforge.jeval.EvaluationException;
133import android.app.Activity;
134import android.os.Bundle;
135import android.view.View;
136import android.widget.Button;
137import android.widget.EditText;
138import android.widget.TextView;
139import android.widget.Toast;
140
141public class ProblemActivity extends Activity {
142
143 private EditText prob;
144 private Button solve;
145
146 @Override
147 protected void onCreate(Bundle savedInstanceState) {
148 super.onCreate(savedInstanceState);
149 setContentView(R.layout.problem);
150
151 // text field
152 prob = (EditText) findViewById(R.id.problem);
153
154 // confirm btn
155 solve = (Button) findViewById(R.id.solve);
156
157 // check if confirm button was pressed
158 solve.setOnClickListener(new View.OnClickListener() {
159
160 @Override
161 public void onClick(View v) {
162
163 // get text from the field
164 String problem = prob.getText().toString();
165
166 // check if expression is not empty
167 if(problem.length() == 0) {
168 // string is empty!
169 Toast.makeText(ProblemActivity.this, getString(R.string.empty_field), Toast.LENGTH_SHORT).show();
170 } else {
171 FormulaModel f = new FormulaModel(ProblemActivity.this);
172 f.setFormulaList("mech");
173 pears.doMagic(problem);
174 try {
175 String str = Global.eval.getVariableValue(Global.UNKNOWN);
176 TextView answ = (TextView) findViewById(R.id.answer);
177 answ.setText(getString(R.string.prob_answ) + str);
178 } catch (EvaluationException e) {
179 Toast.makeText(ProblemActivity.this, getString(R.string.prob_error), Toast.LENGTH_SHORT).show();
180 }
181 }
182 }
183 });
184 }
185
186}
187
188database = dbHelper.getWritableDatabase();