0

I have another problem again :( The error I encountered with this one is that it does not create a table named contacts but I have the code to create database and yet it does not create it.How to solve this? Thank you again for answering.

package com.mobilebasedsignlanguage;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class alphaconv extends Activity {
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alphabetview);

    DataBaseHandler db = new DataBaseHandler(this);
    //get image from drawable..
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.a);

    //convert bitmap to byte
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte imageInByte[] = stream.toByteArray();
    //Inserting Contacts
    Log.d("Insert: ", "Inserting ..");
    db.addContact(new Contact("A", imageInByte));

    //Read all contacts from db
    List<Contact> contacts = db.getAllContacts();
    for(Contact cn: contacts) {
        String log = "ID: " + cn.getID() + "Name: " + cn.getName() + ", Image: " + cn.getImage();

        Log.d("Result: ", log);
        imageArry.add(cn);
    }

    adapter = new ContactImageAdapter(this, R.layout.screen_list, imageArry);
    ListView dataList = (ListView)findViewById(R.id.list);
    dataList.setAdapter(adapter);

}

}

here is the other java classes:

Contact.java

package com.mobilebasedsignlanguage;

public class Contact {

int _id;
String _name;
byte[] _image;

public Contact() {

}

public Contact(int keyId, String name, byte[] image) {
    this._id = keyId;
    this._name = name;
    this._image = image;
}

public Contact(String contactID, String name, byte[] image) {
    this._name = name;
    this._image = image;
}

public Contact(String name, byte[] image) {
    this._name = name;
    this._image = image;
}

public int getID() {
    return this._id;
}

public void setID(int keyId ) {
    this._id = keyId;
}

public String getName() {
    return this._name;
}

public void setName(String name) {
    this._name = name;
}

public byte[] getImage() {
    return this._image;
}

public void setImage(byte[] image) {
    this._image = image;
}

}

ContactImageAdapter.java

package com.mobilebasedsignlanguage;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactImageAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;

ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ImageHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ImageHolder();
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        row.setTag(holder);
    }
    else
    {
        holder = (ImageHolder)row.getTag();
    }

    Contact picture = data.get(position);
    holder.txtTitle.setText(picture._name);

    byte[] outImage = picture._image;
    ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    holder.imgIcon.setImageBitmap(theImage);
    return row;
}

static class ImageHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}


}

DbHandler.java

package com.mobilebasedsignlanguage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "SLdb";

private static final String TABLE_CONTACTS = "contacts";

private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_IMAGE = "image";

public DataBaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_IMAGE + " BLOB" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    onCreate(db);
}

public void addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact._name);
    values.put(KEY_IMAGE, contact._image);

    db.insert(TABLE_CONTACTS, null, values);
    db.close();
}

Contact getContact(int id) {
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
            KEY_NAME, KEY_IMAGE }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null,null,null,null);
    if(cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getBlob(1));

    return contact;
    }

public List<Contact> getAllContacts() {
    List<Contact> contactlist = new ArrayList<Contact>();
    String selectQuery = "SELECT * FROM contacts ORDER BY name";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setImage(cursor.getBlob(2));

            contactlist.add(contact);
        }while (cursor.moveToNext());
    }

    db.close();

    return contactlist;
}

public int updateContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName());
    values.put(KEY_IMAGE, contact.getImage());

    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });      
}

public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
            new String[] {String.valueOf(contact.getID()) });
    db.close();
}

public int getContactsCount() {
    String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}

}

the error at the log cat is this:

02-18 11:49:25.938: E/SQLiteLog(6044): (1) no such table: contacts
02-18 11:49:26.078: E/SQLiteDatabase(6044): Error inserting image=[B@40e29b40 name=A
02-18 11:49:26.078: E/SQLiteDatabase(6044): android.database.sqlite.SQLiteException: no such table: contacts (code 1): , while compiling: INSERT INTO contacts(image,name) VALUES (?,?)
Neha Agarwal
  • 622
  • 7
  • 24
kathleen55
  • 341
  • 2
  • 9
  • 29

2 Answers2

0

Do a log or toast in the onCreate method to see if it's called and if yes then delete your old table as this is called only once and if the table already exists it will not be executed again.

Also, please try using the below query, sometimes spaces in the create statement cause the issue.

 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + " ("
        + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT, "
        + KEY_IMAGE + " BLOB" + ");";
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
0

The problem is that you are not overriding the SQLiteOpenHelper onCreate method, so it is not being called.

Try to put override annotation on the onCreate and onUpgrade methods:

@Override
public void onCreate(SQLiteDatabase db) {
     String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
        + KEY_IMAGE + " BLOB" + ")";
     db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

     onCreate(db);
}
Giacomoni
  • 1,468
  • 13
  • 18
  • i tried to include the override but its the same the table is not found. @giacomoni – kathleen55 Feb 19 '14 at 12:29
  • You can try to change the database version to 2 (with the changes in my answer), then it will recreate your database. Maybe it has not been created for the first time you have run it. – Giacomoni Feb 19 '14 at 18:35