Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
//DBHelper class

public static final String Query="CREATE TABLE "+UserConstruct.newUserinfo.TableName+" ("+UserConstruct.newUserinfo.UserName+" TEXT, "+UserConstruct.newUserinfo.Password+" TEXT,"+
UserConstruct.newUserinfo.FName+" TEXT, "+UserConstruct.newUserinfo.LName+" TEXT,"+UserConstruct.newUserinfo.ACode+" TEXT,"+UserConstruct.newUserinfo.AEmail+" TEXT);";



public DatabaseHelper(Context context)
{
super(context,DatabaseName,null,DatabaseVersion);
Log.e("Database operation","created or opened");

}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(Query);
Log.e("Created","Table Created");
File f=new File("/data/data/com.example.future.buildingmonitoringsystem/databases/ptl2_DB.db");
FileInputStream fis=null;
FileOutputStream fos=null;

try
{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/ptl2_DB.db");
while(true)
{
int i=fis.read();
if(i!=-1)
{fos.write(i);}
else
{break;}
}
fos.flush();


//code for insertion in main class

String UserName="admin"
Password = "password";
String FName="abc";
String LName="abc";
String ACode="1234";
String AEmail="sjn@gmail.com";
String TableName="htl_portal_user";

dbhelper=new DatabaseHelper(context);
sqlitedatabase=dbhelper.getWritableDatabase();
dbhelper.insert( UserName,Password,FName,LName,ACode,AEmail,sqlitedatabase);
Toast.makeText(getBaseContext(),"Data inserted",Toast.LENGTH_LONG).show();
dbhelper.close();

What I have tried:

I can see my db in device monitor, i can insert and access 1st row. I can insert second row also, but after the insertion i cant access db, app is hanging and stop.
And i export that db file and check the table, I can see that second row is a copy of first row(elements that i inserted first). Can any one provide a solution for this? My code for table creation and insertion are given below.
Posted
Comments
wseng 1-Oct-16 11:30am    
can you post the error when your app crashed ?
Suvendu Shekhar Giri 1-Oct-16 15:23pm    
"My code for table creation and insertion are given below"
Where?
David Crow 7-Jun-17 22:33pm    
The preferred method for database I/O on an Android platform is to extend SQLiteOpenHelper. Are you avoiding that for a reason?

As to your question, both of the rows are the same because the call to dbhelper.insert() is the same for both. Is that intentional?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900