Alfred
Alfred

Reputation: 101

SQLite Exception Data insertion issue for columns

Here is my code for the dbHelper class:

package com.example.emp_management;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {
    super(context, dbName, null, 1);
    // TODO Auto-generated constructor stub
}
static final String dbName="EmployeeManagementSystem";
static final String Login_Table="Login_Authentication";
static final String colID="ID";
static  final String colUsername="Username";
static final String colPassword="Passwrod";

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//db.execSQL("CREATE TABLE" + Login_Table+ "(" +
    //       colID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
        //  colUsername + "TEXT NOT NULL," +
          // colPassword + "TEXT NOT NULL);"                    
            //);
//db.execSQL("CREATE TABLE "+Login_Table+" ("+colID+ " INTEGER PRIMARY KEY AUTOINCREMENT ,"+
    //    colUsername+ " TEXT ,"+ colPassword + "TEXT");
db.execSQL("CREATE TABLE " + Login_Table + "(colID INTEGER PRIMARY KEY AUTOINCREMENT, colUsername TEXT NOT NULL,colPassword TEXT NOT NULL);");
Log.w("Come aww man", "Database Table Created!!");


}
    @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + "Login_Authentication");
onCreate(db);
}
public void insert_new_user(String username,String password)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(DatabaseHelper.colUsername,username);
    cv.put(DatabaseHelper.colPassword,password);
    db.insert(Login_Table, null, cv);
    db.close();
    }
}

Here is the code am using to add new employee into the table i have created:

 package com.example.emp_management;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Adding_Employee extends Activity{

@Override
protected void onCreate(Bundle aglakaam) {
    // TODO Auto-generated method stub
    super.onCreate(aglakaam);

    setContentView(R.layout.add_employee);
    final EditText new_user = (EditText) findViewById(R.id.editText1);
    final EditText new_pass = (EditText) findViewById(R.id.editText2);
    final Button create_acc = (Button) findViewById(R.id.creat_acc);

    create_acc.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        DatabaseHelper accessing_db = new DatabaseHelper(Adding_Employee.this);
        accessing_db.insert_new_user(new_user.getText().toString(), new_pass.getText().toString());
        Toast.makeText(getApplicationContext(), "New User Has Been Created!!", Toast.LENGTH_SHORT).show();

    }
});
}


}

And here is my logcat:

02-20 02:42:26.491: E/Database(384): Error inserting Passwrod=1234 Username=sjdf
02-20 02:42:26.491: E/Database(384): android.database.sqlite.SQLiteException: table     Login_Authentication has no column named Passwrod: , while compiling: INSERT INTO Login_Authentication(Passwrod, Username) VALUES(?, ?);
02-20 02:42:26.491: E/Database(384):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)

Something wierd here to me here is that the column of username exists. Why is it only showing exception for password and not username and even when password column exists why is the exception thrown here. Kindly help me!

Upvotes: 0

Views: 163

Answers (3)

noname
noname

Reputation: 271

Maybe you just need space after colID , colUsername , colPassword

db.execSQL("CREATE TABLE " + Login_Table + "(" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + colUsername + " TEXT NOT NULL," + colPassword + " TEXT NOT NULL);");

Upvotes: 1

dymmeh
dymmeh

Reputation: 22306

You are creating columns with the names colPassword , colUsername , and colID

db.execSQL("CREATE TABLE " + Login_Table + "(colID INTEGER PRIMARY KEY AUTOINCREMENT, colUsername TEXT NOT NULL,colPassword TEXT NOT NULL);");

and then trying to access them using

static final String colID="ID";
static  final String colUsername="Username";
static final String colPassword="Passwrod"; 

Since Passwrod != colPassword .. of course you won't be able to find that column.

Try something like this to ensure you are using the same names for columns when creating the table along with accessing it. You'll need to increase your DB version in order for the changes to show.

db.execSQL("CREATE TABLE " + Login_Table + "(" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + colUsername + " TEXT NOT NULL," + colPassword + " TEXT NOT NULL);");

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93569

You misspelled Password as Passwrod. Simple typo. Also, Java doesn't do variable string interpolation, you need to use concatenation.

Upvotes: -1

Related Questions