im new on Android. I have some trouble with the insert statement in the database, when im running the application the values have not been inserted. Please someone can help.
Try to understand this code will help you to understand in better way:
package com.example.lalit.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.method.HideReturnsTransformationMethod;
import android.util.Log;
import android.widget.Toast;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Currency;
/**
* Created by lalit on 7/30/2015.
*/
public class HotOrNot {
public final static String KEY_ROWID="_id";
public final static String KEY_NAME="persion_name";
public final static String KEY_HOTNESS="persion_hotness";
public final static String KEY_SUB="OS";
public final static String DATABASE_NAME="HotOrNotdb";
public final static String DATABASE_TABLE="peopleTable";
public final static int DATABASE_VERSION=1;
public static Dbhelper ourHelper;
public final Context ourContext;
public SQLiteDatabase ourDatabase;
private static final String DATABASE_CREATE = "create table peopleTable(_id integer primary key autoincrement, "
+ "persion_name text not null, persion_hotness text not null);";
public class Dbhelper extends SQLiteOpenHelper{
public Dbhelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
/* db.execSQL("CREATE TABLE" + DATABASE_NAME +"("+
KEY_ROWID +"INTEGER PRIMARY KEY AUTOINCREMENT,"+
KEY_NAME +"TEXT NOT NULL,"+
KEY_HOTNESS+"TEXT NOT NULL);"*/
//);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST "+DATABASE_TABLE);
onCreate(db);
}
}
public HotOrNot(Context c)
{
ourContext=c;
}
public HotOrNot open() throws SQLException
{
ourHelper=new Dbhelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long createEntry(String name,String hotness)
{
ourDatabase.execSQL("INSERT INTO peopleTable (persion_name,persion_hotness) VALUES('Lalit','Kushwah')");
ContentValues cv=new ContentValues();
cv.put(KEY_NAME,name);
cv.put(KEY_HOTNESS,hotness);
return ourDatabase.insert(DATABASE_TABLE,null,cv);
}
public String getData()
{
String[] col=new String[]{KEY_ROWID,KEY_NAME,KEY_HOTNESS};
Cursor c=ourDatabase.query(DATABASE_TABLE,col,null,null,null,null,null);
String result="";
int irow=c.getColumnIndex(KEY_ROWID);
int iname=c.getColumnIndex(KEY_NAME);
int ihot=c.getColumnIndex(KEY_HOTNESS);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
result=result+c.getString(irow)+" "+c.getString(iname)+" "+c.getString(ihot)+ "\n";
}
return result;
}
}