Monday, February 6, 2012

[android-developers] SQLiteTable is not inserting

package Database.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class DatabaseLogin extends Activity {
private EditText edit;
private Button login;
private DBAdapter db;
private Button register;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();

}

private void setUpViews() {

//add the user names and passwords
//db.open();
//long id = db.insertUser("srikanth", "srikanth");
//db.close();


edit = (EditText)findViewById(R.id.edit);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String username = edit.getText().toString();
String password = edit.getText().toString();


}
});

register = (Button)findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),Register.class);
startActivity(in);
}
});
}


}


package Database.login;

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

public class DBAdapter {


public static final String KEY_ROWID = "_id";

public static final String KEY_NAME = "username";
public static final String KEY_PASS = "password";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "DB";

public static final String DATABASE_CREATE =
"create table login (_id integer primary key autoincrement,"+
"+name text not null,password text not null)";
private DatabaseHelper helper;
private SQLiteDatabase db;
private Context context;

public DBAdapter(Context ctx)
{
this.context = ctx;
helper = new DatabaseHelper(context);
}
class DatabaseHelper extends SQLiteOpenHelper
{

public DatabaseHelper(Context context) {
// TODO Auto-generated constructor stub
super(context,DATABASE_NAME , null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(DATABASE_CREATE);
}
catch (Exception e) {
// TODO: handle exception
}

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// TODO Auto-generated method stub

}
}
//open the data base
public DBAdapter open() throws SQLException{
db = helper.getWritableDatabase();
return this;
}
//close
public void close(){
db.close();
}

//insert user name and password
public long insertUser(String name,String password)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_PASS, password);

return db.insert(DATABASE_CREATE, null, initialValues);
}

}


package Database.login;

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;

public class Register extends Activity {

private Button agree;
private EditText userName;
private EditText pass;
DBAdapter db = new DBAdapter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
userName = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
agree = (Button)findViewById(R.id.agree);

agree.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

String userName1 = userName.getText().toString();
String password1 = pass.getText().toString();
db.open();
long id = db.insertUser(userName1, password1);
db.close();

}
});
}

}

i have created the respective layout xml files namely
main.xml,register.xml .
But table is not inserting.
please help.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment