I have created an SQlite table to save users but i am getting an exception when trying to add a user: E/SQLiteLog: (1) no such table: users in "INSERT INTO users(lastName,age,firstName) VALUES (?,?,?)"
the database handler class:
package com.example.as.Data;
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log;
import com.example.as.Model.User; import com.example.as.util.Util;
import java.util.ArrayList; import java.util.List;
public class DatabaseHandler extends SQLiteOpenHelper {
//the name is the name of the database we will be creating public DatabaseHandler(Context context) { super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_USERS_TABLE = "CREATE TABLE " Util.TABLE_NAME "(" Util.KEY_ID " INTEGER PRIMARY KEY," Util.lastName " TEXT," Util.lastName " TEXT," Util.age " TEXT" ")"; db.execSQL(CREATE_USERS_TABLE); } //this is called when you upgrade your database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String DROP_TABLE = String.valueOf("DROP TABLE IF EXISTS"); db.execSQL(DROP_TABLE, new String[]{Util.DATABASE_NAME}); onCreate(db); } //how to add to database public void addContact(User user){ //we will use the contact obj to get our shit from content class SQLiteDatabase db = this.getWritableDatabase(); //structure that allows us to create the items to be added to the database ContentValues values = new ContentValues(); values.put(Util.firstName, user.getFirstName()); //putting in our values values.put(Util.lastName, user.getLastName()); values.put(Util.age, user.getAge()); //inserting the ContentValues into the table db.insert(Util.TABLE_NAME, null, values); Log.d("DBHandler", "addContact: " "item added"); db.close(); } //get a contact by using their id number /* public User getUser(int id){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(Util.TABLE_NAME,new String[]{Util.KEY_ID, Util.firstName, Util.lastName, Util.age}, Util.KEY_ID "=?",new String[]{String.valueOf(id)}, null,null,null); if (cursor!=null) cursor.moveToFirst(); //start iterating through //creating and populating the contact obj that we will return User user = new User(); user.setId(Integer.parseInt(cursor.getString(0))); user.setFirstName(cursor.getString(1)); //these are row numbers user.setLastName(cursor.getString(2)); //0 is the id row and 1 is the name row user.setAge(cursor.getString(3)); return user; }*/ //lets get all of the contacts public List<User> getAllContacts(){ List<User> userList = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); //SQL string to select all contacts String selectAll = "SELECT * FROM " Util.TABLE_NAME; //rawquery built in method allows you to pass in raw sql code Cursor cursor = db.rawQuery(selectAll,null); //iterate through the data if(cursor.moveToFirst()){ do { User user = new User(); user.setId(Integer.parseInt(cursor.getString(0))); user.setFirstName(cursor.getString(1)); user.setLastName(cursor.getString(2)); user.setAge(cursor.getString(3)); //add the contact objects to the list userList.add(user); }while (cursor.moveToNext()); } return userList; }
}
the util class:
> package com.example.as.util;
public class Util {
//database related items (write psf for the options to show up)
public static final int DATABASE_VERSION = 1; //every database needs to have a version
public static final String DATABASE_NAME = "users_db";
public static final String TABLE_NAME = "users"; //we need a table name
//column names
public static final String KEY_ID = "id";
public static final String firstName = "firstName";
public static final String lastName = "lastName";
public static final String age = "age";
}
Main Activity:
package com.example.as;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.as.Data.DatabaseHandler;
import com.example.as.Model.User;
import org.w3c.dom.Text;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button addUserbtn;
private EditText firstNametxt;
private EditText lastNametxt;
private EditText agetxt;
private TextView userstxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(MainActivity.this);
addUserbtn = findViewById(R.id.aduserbtn);
firstNametxt= findViewById(R.id.FirstNametxt);
lastNametxt=findViewById(R.id.lastNametxt);
agetxt = findViewById(R.id.agetxt);
userstxt = findViewById(R.id.userstxt);
addUserbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String firstName = firstNametxt.getText().toString();
String lastName = lastNametxt.getText().toString();
String age = agetxt.getText().toString();
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setAge(age);
db.addContact(user);
// List<User> userList =db.getAllContacts(); //adding all users to list
// for (User us: userList
// ) {
Log.d("MainActivity","onCreate" user.getFirstName());
//}
}
});
}
//changing to all Users Screen
public void ChangeScenes(View view) {
Intent intent = new Intent(this, allUsers.class);
startActivity(intent); //now we use the intent we made before
}
}
CodePudding user response:
1.Upgrade Your DataBase Version number
Or
2.First Uninstall
Your Apps from your Emulator or Phone ad Re-install it.
In this way, I think you can solve your problem.
CodePudding user response:
I think you have an issue in Create table Query Please check it and before Add Data check in "App Inspector" Is Your Table created or not?
if Table create. then You Need To upgrade Database Version.
Your Create Table Query
String CREATE_USERS_TABLE = "CREATE TABLE " Util.TABLE_NAME "("
Util.KEY_ID " INTEGER PRIMARY KEY," Util.lastName " TEXT,"
Util.lastName " TEXT," Util.age " TEXT" ")";
db.execSQL(CREATE_USERS_TABLE);
Try This One
String CREATE_USERS_TABLE = "CREATE TABLE " Util.TABLE_NAME "("
Util.KEY_ID " INTEGER PRIMARY KEY," Util.firstName " TEXT,"
Util.lastName " TEXT," Util.age " TEXT" ")";
db.execSQL(CREATE_USERS_TABLE);
You face this issue because you wrote the last name two times in create table query.