I have searched StackOverFlow and found no solution for my problem.
SQLite Exception: no such table Error
Android SQLite "no such table" exception
My Database Code:
package com.example.mylifeinformationsharedsocial;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyUsersDatabase extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "usersDatabase.db"; // Name of the file i save on the device
private static final String TABLE_NAME = "usersTable"; // Name of the table
// User columns
private static final String UID = "_id"; // User identification number column
private static final String FIRST_NAME = "firstName"; // User first name column
private static final String LAST_NAME = "lastName"; // User last name column
private static final String CREATE_TABLE = "CREATE TABLE " + MyUsersDatabase.DATABASE_NAME + " ( "
+ UID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MyUsersDatabase.FIRST_NAME + " TEXT, "
+ MyUsersDatabase.LAST_NAME + " TEXT" + ");";
private static final String DROP_TABLE = "DROP TABLE IF EXIST " + TABLE_NAME;
public MyUsersDatabase(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating tables
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(CREATE_TABLE);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
try
{
db.execSQL(DROP_TABLE);
onCreate(db);
Log.d("OnCreate", "OnCreate called");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void addUser(User user) // Add a new row to the users table
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MyUsersDatabase.UID,user.getId());
contentValues.put(MyUsersDatabase.FIRST_NAME,user.getFirstName());
contentValues.put(MyUsersDatabase.LAST_NAME,user.getLastName());
db.insert(MyUsersDatabase.TABLE_NAME, null, contentValues);
db.close(); // Closing database connection
}
public void deleteUser(int userId) // Delete a row from the users table
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELTE FROM " + DATABASE_NAME + " WHERE " + UID + "=\"" + userId + "\";");
db.close(); // Closing database connection
}
public User getUser(int userId)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(MyUsersDatabase.TABLE_NAME, new String[] {MyUsersDatabase.UID,MyUsersDatabase.FIRST_NAME,
MyUsersDatabase.LAST_NAME},MyUsersDatabase.UID + "=?",
new String[] {String.valueOf(userId)}, null, null, null, null);
if (cursor != null)
{
cursor.moveToFirst();
}
User user = new User(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2));
return user;
}
}
My Object(User) Class:
package com.example.mylifeinformationsharedsocial;
public class User
{
private int _id;
private String firstName;
private String lastName;
public User(int id,String firstName,String lastName)
{
this._id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public User()
{
}
public int getId()
{
return _id;
}
public void setId(int id)
{
this._id = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String toString()
{
return "ID - " + this._id + " ,First name - " + this.firstName + " ,Last name - " + this.lastName;
}
}
My Activity:(where I'm trying to add a row to the table database and get the SQLiteException: no such table
)
package com.example.mylifeinformationsharedsocial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class SignUpActivity extends AppCompatActivity
{
Button signUpButton;
EditText firstNameEditText;
EditText lastNameEditText;
EditText usernameEditText;
EditText passwordEditText;
EditText emailEditText;
MyUsersDatabase myUsersDatabase;
String userFirstName;
String userLastName;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
viewsInitialization();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
myUsersDatabase = new MyUsersDatabase(SignUpActivity.this);
signUpButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
myUsersDatabase.addUser(new User(0,"Dan","Riman")); // Here the exception
firstNameEditText.setText(myUsersDatabase.getUser(0).toString());
}
});
}
private void viewsInitialization()
{
signUpButton = (Button) findViewById(R.id.sign_up_button_id);
firstNameEditText = (EditText) findViewById(R.id.sign_up_first_name_edit_text_id);
lastNameEditText = (EditText) findViewById(R.id.last_name_edit_text_id);
usernameEditText = (EditText) findViewById(R.id.username_edit_text_id);
passwordEditText = (EditText) findViewById(R.id.password_edit_text_id);
emailEditText = (EditText) findViewById(R.id.email_edit_text_id);
}
}
So when i execute the line myUsersDatabase.addUser(new User(0,"Dan","Riman"));
i get the SQLiteException: no such table when trying to add an Object to a table
.
LogCat:
11-27 15:51:58.567: E/SQLiteLog(20624): (1) no such table: usersTable
11-27 15:51:58.568: D/AndroidRuntime(20624): Shutting down VM
11-27 15:51:58.569: E/AndroidRuntime(20624): FATAL EXCEPTION: main
11-27 15:51:58.569: E/AndroidRuntime(20624): Process: com.example.mylifeinformationsharedsocial, PID: 20624
11-27 15:51:58.569: E/AndroidRuntime(20624): android.database.sqlite.SQLiteException: no such table: usersTable (code 1): , while compiling: SELECT _id, firstName, lastName FROM usersTable WHERE _id=?
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConne ction.java:894)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:505)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.jav a:1417)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1 264)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1135)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1341)
11-27 15:51:58.569: E/AndroidRuntime(20624): at com.example.mylifeinformationsharedsocial.MyUsersDatabase.getUser(MyUsersData base.java:93)
11-27 15:51:58.569: E/AndroidRuntime(20624): at com.example.mylifeinformationsharedsocial.SignUpActivity$1.onClick(SignUpActi vity.java:61)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.view.View.performClick(View.java:4763)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.view.View$PerformClick.run(View.java:19821)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.os.Handler.handleCallback(Handler.java:739)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.os.Looper.loop(Looper.java:135)
11-27 15:51:58.569: E/AndroidRuntime(20624): at android.app.ActivityThread.main(ActivityThread.java:5274)
11-27 15:51:58.569: E/AndroidRuntime(20624): at java.lang.reflect.Method.invoke(Native Method)
11-27 15:51:58.569: E/AndroidRuntime(20624): at java.lang.reflect.Method.invoke(Method.java:372)
11-27 15:51:58.569: E/AndroidRuntime(20624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:90 9)
11-27 15:51:58.569: E/AndroidRuntime(20624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
So, That's about it. Thank for the help.
Aucun commentaire:
Enregistrer un commentaire