dimanche 9 août 2015

android sqlite exception no such table found while 'insert into contacts' android

in my SQLite adapter class

public class SqLiteAdapter
{

MySqliteHelper helper;
Context context;

SqLiteAdapter(Context context)
{
this.context = context;
helper = new MySqliteHelper(context);
}


public long insert(String first, String last, int num)
{
SQLiteDatabase database = helper.getWritableDatabase();
Toast.makeText(context, "database found", Toast.LENGTH_SHORT).show();
ContentValues values = new ContentValues();

values.put(MySqliteHelper.COLUMN_FIRST, first);
values.put(MySqliteHelper.COLUMN_LAST, last);
values.put(MySqliteHelper.COLUMN_PHO_NUM, num);

long id = database.insert(MySqliteHelper.TABLE_NAME, null, values);
return id;

}





 static class MySqliteHelper extends SQLiteOpenHelper
{
Context context;
private static final String DATABASSE_NAME = "contact";
private static final int VERSION = 1;
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_FIRST = "firstName";
private static final String COLUMN_LAST = "lastName";
private static final String COLUMN_PHO_NUM = "phoneNumber";
private static final String CREATE_TABLE = "create table" +TABLE_NAME +
"(" + COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_FIRST + "TEXT, " + COLUMN_LAST + "TEXT," + COLUMN_PHO_NUM +       "INTEGER"     + ");";




MySqliteHelper(Context context)
{
super(context, DATABASSE_NAME, null, 1);
Toast.makeText(context, "helper class created", Toast.LENGTH_SHORT).show();
this.context = context;
}


@Override
public void onCreate(SQLiteDatabase database)
{
Toast.makeText(context, "on create called", Toast.LENGTH_SHORT).show();
try
 {
database.execSQL(CREATE_TABLE);
} catch (SQLException e){
Toast.makeText(context, "Failed Creating Table", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}


}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int  newVersion)
{
database.execSQL("DROP TABLE IF EXIST" + TABLE_NAME);
onCreate(database);
}

   }
}

in my MainAcivity

public class MainActivity extends AppCompatActivity
{
EditText enterFirstName;
EditText enterLastName;
EditText enterNumber;



@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterFirstName = (EditText) findViewById(R.id.enterFirstName);
enterLastName = (EditText) findViewById(R.id.enterLastName);
enterNumber = (EditText) findViewById(R.id.enterNumber);


}


public void addContact(View view)
{
String first = enterFirstName.getText().toString();
  String last = enterLastName.getText().toString();
 int num = Integer.parseInt(enterNumber.getText().toString());
SqLiteAdapter adapter = new SqLiteAdapter(this);
 long id = adapter.insert(first, last, num);
}

and the error "no such table contacts (code1) while compiling: insert into contacts etc.."

i just dove into database storage for my app (oh lucky me) i've been getting this error with all the tutorials i've followed through on, and now even in my own code. researching this topic it seemed that i was trying to write into the database before it was created, so i set up Toasts to track the flow and it it's progression didn't seem like i was writing to it before it was created, but idk i'm a beginner. so why exactly do you guys think i'm getting this error? i apologize in advance for the sloppy editing, and thank you in advance for any help so i can start writing better code =D!

Aucun commentaire:

Enregistrer un commentaire