alighorbani alighorbani - 3 months ago 14
Android Question

Creating and inserting into a database

I'm trying to create a basic database and insert three users, but it doesn't work.

This is a class that creates the database

package com.example.matadoor.db1;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by matadoor on 06/08/2016.
*/
public class dbcreat extends SQLiteOpenHelper {

public final String tbname="t1";
public final String cid="id";
public static String cid2="test";
public final String user="username";

public final String creat="CREATE TABLE "+cid2+"("+cid+"INTEGER primery key AUTO_INCREMENT , "+user+" TEXT); ";

public dbcreat(Context context) {
super(context,cid2,null, 1);
}

public dbcreat(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(creat);
}

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

}


And it's the other class which handles the database - opens it, closes it, and inserts into it

package com.example.matadoor.db1;

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

/**
* Created by matadoor on 08/08/2016.
*/
public class dbhandler {

private dbcreat dbc;
private SQLiteDatabase sdb;

public dbhandler(Context context)
{
dbc=new dbcreat(context);
}

public void open()
{
sdb=dbc.getWritableDatabase();
}

public void close(){
dbc.close();
}

public String display(int row)
{
Cursor cu=sdb.query(dbc.tbname,null,null,null,null,null,null);
cu.moveToPosition(row);
String user=cu.getString(1);
return user;
}


public void insert(String user)
{
ContentValues cn=new ContentValues();
cn.put(dbc.user,user);
}
}


And the main class which has one Button for inserting a user into the db

package com.example.matadoor.db1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends AppCompatActivity {

Button btn;
dbhandler db;

dbcreat dbc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn=(Button)findViewById(R.id.ins);

// dbc.getWritableDatabase();
db.open();

db=new dbhandler(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String [] user={"ali","reza","ahmed"};

for(int i=0;i<user.length;i++)
{
db.insert(user[i]);
}
}
});

}
}

Answer

So this seems to be a error. You need to switch those two lines.

    db.open(); // NullPointer here

    db=new dbhandler(this);

Assuming there are no other errors with the code, this method is doing nothing to the database.

public void insert(String user)
{
    ContentValues cn=new ContentValues();
    cn.put(dbc.user,user);
}

You have to call sdb.insert with that cn variable.

Comments