i am new to android, and now i am developing one chat application.
i have difficulties to take results from the database.
When i execute the select query i always receive the following error:
"An error has occurred in lmu.de.exercise3. no such table: chat, while
compiling:
Using sql Syntax Highlighting
- SELECT COUNT(*) FROM (SELECT * FROM chat WHERE date = '2008-04-11')"
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Here is my code:
Using java Syntax Highlighting
- package lmu.de.excercise3;
- import java.lang.String;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Spinner;
- import android.widget.ArrayAdapter;
- import android.widget.TextView;
- import android.widget.Button;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.content.SharedPreferences;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.AdapterView;
- import android.database.sqlite.SQLiteDatabase;
- import java.io.FileNotFoundException;
- import java.util.Calendar;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.util.Log;
- public class Exercise3 extends Activity {
- public TextView tv;
- public EditText et;
- public Button but, dayButton, allButton;
- public String start = "Chat starting...\n";
- public int selection;
- public SharedPreferences settings;
- private int _year;
- private int _month;
- private int _day;
- private int _hour;
- private int _minute;
- public static final String DBASE = "chat.db";
- public static final String TABLE = "history";
- public SQLiteDatabase db = openDB();
- private String createTable = "CREATE TABLE " + TABLE + " (_id INTEGER PRIMARY KEY autoincrement, message TEXT, date TEXT, time TEXT); ";
- private String insert, select, date, time;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- tv = (TextView)findViewById(R.id.text);
- et = (EditText)findViewById(R.id.message);
- but = (Button)findViewById(R.id.ok);
- dayButton = (Button)findViewById(R.id.day);
- allButton = (Button)findViewById(R.id.all);
- Spinner s1 = (Spinner) findViewById(R.id.status);
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.status, android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- s1.setAdapter(adapter);
- tv.setText(start);
- settings = getSharedPreferences("Preferences", MODE_PRIVATE);
- selection = settings.getInt("status", 1);
- s1.setSelection(selection);
- s1.setOnItemSelectedListener(new OnItemSelectedListener() {
- SharedPreferences.Editor editor = settings.edit();
- public void onItemSelected(AdapterView parent, View v, int position, long id) {
- switch (position) {
- default:
- editor.putInt("status", position);
- editor.commit();
- break;
- }
- }
- public void onNothingSelected(AdapterView parent) {
- }
- });
- init();
- but.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if (et.length() != 0) {
- Calendar c = Calendar.getInstance();
- _year = c.get(Calendar.YEAR);
- _month = c.get(Calendar.MONTH);
- _day = c.get(Calendar.DAY_OF_MONTH);
- _hour = c.get(Calendar.HOUR);
- _minute = c.get(Calendar.MINUTE);
- date = _year + "-" + _month + "-" + _day;
- time = _hour + ":" + _minute;
- tv.setText(tv.getText() + "\n" + "(" + time + ") me: " + et.getText());
- et.setText("");
- //insert = "insert into chat values (null, '" + et.getText() + "', '" + date + "', '" + time + "')";
- //db.execSQL(insert);
- ContentValues values = new ContentValues();
- values.put("message", et.getText().toString());
- values.put("date", date);
- values.put("time", time);
- db.insert(TABLE, null, values);
- //db.close();
- }
- }
- });
- dayButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Calendar c = Calendar.getInstance();
- _year = c.get(Calendar.YEAR);
- _month = c.get(Calendar.MONTH);
- _day = c.get(Calendar.DAY_OF_MONTH);
- date = _year + "-0" + _month + "-" + _day;
- time = _hour + ":" + _minute;
- select = "SELECT * FROM " + TABLE + " WHERE date = '" + date + "'";
- /*
- String where = "date = '" + date + "'";
- //String order = "date asc";
- String selections[] = {"message", "time"};
- Cursor cur = db.query(TABLE, selections, where, null, null, null, null);
- */
- Cursor cur = db.rawQuery(select, null);
- if(cur != null) {
- int num = cur.getColumnIndex("message");
- String msg = cur.getString(num);
- tv.setText(msg);
- }
- else {
- tv.setText("No history available for today.");
- }
- //db.close();
- }
- });
- }
- public SQLiteDatabase openDB() {
- try {
- return this.openDatabase(DBASE, null);
- }
- catch( Exception e ) {
- return null;
- }
- }
- public void init() {
- try {
- db = this.openDatabase(DBASE, null);
- }
- catch(FileNotFoundException e) {
- try {
- db = this.createDatabase(DBASE, 1, MODE_PRIVATE, null);
- try {
- db.execSQL(createTable);
- Log.e("DEBUGTAG","Error occured", e);
- }
- catch(Exception exc) {
- Log.e("DEBUGTAG","Error occured", exc);
- }
- }
- catch(FileNotFoundException ex) {
- db = null;
- }
- }
- }
- }
Parsed in 0.057 seconds, using GeSHi 1.0.8.4



