Inserting values from a textbox into SQLite database

I've created an Android application which requires the user to register his username and password. My question is, how can I insert the input values(textbox) into my SQLite database?

Thanks in advance.

Answers


First, read Android docs on SQLite if you haven't already http://developer.android.com/guide/topics/data/data-storage.html#db.

Then assuming you have everything setup properly on the DB side the next step is to construct your SQL to look something like this...

private static final String INSERT_SQL = "insert into " + TABLE + " (username, password) " + 
                                                      "   values    (?,        ?)";

Then you need to actually do the insert...

public void doInsert(String username, String password) {
    final SQLiteDatabase  db = getWritableDatabase();

    SQLiteStatement insert_stmt = null;

    try {
        insert_stmt = db.compileStatement(INSERT_SQL);

        insert_stmt.bindString(1,   username);
        insert_stmt.bindString(2,   password);
        insert_stmt.executeInsert();
    }
    finally {
        if (insert_stmt != null) insert_stmt.close();
    }
}

This assumes doInsert is located in a class that extends SQLiteOpenHelper so that it can use getWritableDatabase, but as long as you have your SQLiteOpenHelper around you can move doInsert anywhere you like.

If you need to get values from an EditText then this should get you going...

final EditText input = (EditText) findViewById(R.id.textId);
final String   txt   = input.getText().toString();

Need Your Help

mod_rewrite Friendly URL clashing with 404

php .htaccess mod-rewrite http-status-code-404 http-status-code-301

I've been struggling with my mod_rewrite for a while now and I thought I'd cracked it…

Annotating sensor data but no rdf:type

java rdf sparql ontology jena

I'm annotating sensor observations using JENA, RDF and the W3C SSNXG's sensor ontology.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.