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

How to get access to Microsoft.Phone.Control in a Portable Class Library

c# .net windows-phone-8 portable-class-library

I need to create a library for WP 8+ Apps that will recognize touches and gestures (and storing info about that). Is there a way to do this using Portable Class Library?

Saving first and last frame of a video as an image on Android

android video frame

I want to save the first and last frame of a video as an images.