lundi 30 novembre 2015

database helper not able to create database

I am a beginner to android.

While trying to implement a SQLite database, I'm unable to insert into the database.
I'm also unable to figure out what is wrong with my database helper?

here is my helper class code with methods for insering and finding a user

package com.example.tilmac.dbsql;

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

/**
 * Created by TILMAC on 28-11-15.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "User.db";
    private static final String TABLE_NAME = "users";       //final variables
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_UNAME = "uname";
    private static final String COLUMN_PASS = "pass";
    SQLiteDatabase db;


    public DatabaseHelper(Context context)              //Constructor
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

The CREATE TABLE statement is hardcoded:

private static final String TABLE_CREATE = "create table users (name text not null, email text not null, uname text not null, pass text not null)";

I get an error which says Column not found while inserting into database

    public void insertUser(Contact c){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, c.getName());
        values.put(COLUMN_EMAIL, c.getEmail());
        values.put(COLUMN_UNAME, c.getUname());
        values.put(COLUMN_PASS, c.getPass());

        db.insert(TABLE_NAME, null, values);
        db.close();

    }

    public String findPass(String s){
        db = this.getReadableDatabase();
        String query = "select uname, pass from "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a, b="not found";

        if(cursor.moveToFirst())
        {
            do{
                a = cursor.getString(0);
                if(a.equals(s))
                {
                    b=cursor.getString(1);
                    break;
                }
            }while(cursor.moveToNext());
        }
        return b;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {                               //OnCreate method

        db.execSQL(TABLE_CREATE);
        this.db = db;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {      //onUpgrade Method

        String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);
    }
}

//and here is my main activity

package com.example.tilmac.dbsql;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by TILMAC on 27-11-15.
 */
public class RegisterActivity extends AppCompatActivity {

    DatabaseHelper helper = new DatabaseHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ///Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
    }

    public void onRBClick(View v){
        if(v.getId()==R.id.Rbutton)
        {
            EditText n = (EditText)findViewById(R.id.RETname);
            EditText e = (EditText)findViewById(R.id.RETemail);
            EditText u = (EditText)findViewById(R.id.RETuname);
            EditText p1 = (EditText)findViewById(R.id.RETpass);
            EditText p2 = (EditText)findViewById(R.id.RETrepass);

            String name, email, uname, pass, repass;
            name = n.getText().toString();
            email = e.getText().toString();
            uname = u.getText().toString();
            pass = p1.getText().toString();
            repass = p2.getText().toString();

            if(pass.equals(repass))
            {
                Contact c = new Contact();
                c.setName(name);
                c.setEmail(email);
                c.setUname(uname);
                c.setPass(pass);

                helper.insertUser(c);

            }
            else
            {
                Context context = getApplicationContext();
                Toast toast = Toast.makeText(context, "Passwords dont match", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire