Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1111515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:33:34+00:00 2026-05-17T02:33:34+00:00

I know you can use python and other scripting languages in android. But I

  • 0

I know you can use python and other scripting languages in android. But I haven’t seen weather or not it was possible to use python as an interface to sqlite in android. Is this possible? This is the first android app where I’ve needed sqlite, and using the java api’s is retarded.

If this isn’t possible, can someone point me to a good tutorial on sqlite in android? I’ve found a bunch, but all of them are entirely different and I’m totally lost on which is the best way to do it.

It’s just hard to picture how google expects you to use the sqlite database. It seems like you need like 10 different classes just to query a database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:33:35+00:00Added an answer on May 17, 2026 at 2:33 am

    Actually you just need 3 classes:

    A ContentProvider, as found here: http://developer.android.com/guide/topics/providers/content-providers.html

    Second you need is a SQLiteOpenHelper and last but not least a Cursor

    Edit: Just noticed it’s not obvious from the snippets what the db variable is. It’s the SQLiteOpenHelper or better my extension of it (where I’ve only overridden the onCreate, onUpgrade and constructor. See below ^^

    The ContentProvider is the one which will be communicating with the database and do the inserts, updates, deletes. The content provider will also allow other parts of your code (even other Apps, if you allow it) to access the data stored in the sqlite.

    You can then override the insert/delete/query/update functions and add your functionality to it, for example perform different actions depending on the URI of the intent.

    public int delete(Uri uri, String whereClause, String[] whereArgs) {
        int count = 0;
    
        switch(URI_MATCHER.match(uri)){
        case ITEMS:
            // uri = content://com.yourname.yourapp.Items/item
            // delete all rows
            count = db.delete(TABLE_ITEMS, whereClause, whereArgs);
            break;
        case ITEMS_ID:
            // uri = content://com.yourname.yourapp.Items/item/2
            // delete the row with the id 2
            String segment = uri.getPathSegments().get(1);
            count = db.delete(TABLE_ITEMS, 
                    Item.KEY_ITEM_ID +"="+segment
                    +(!TextUtils.isEmpty(whereClause)?" AND ("+whereClause+")":""),
                    whereArgs);
            break;
        default:
            throw new IllegalArgumentException("Unknown Uri: "+uri);
        }
    
        return count;
    }
    

    The UriMatcher is defined as

    private static final int ITEMS = 1;
    private static final int ITEMS_ID = 2;
    private static final String AUTHORITY_ITEMS ="com.yourname.yourapp.Items";
    private static final UriMatcher URI_MATCHER;
    
    static {
        URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        URI_MATCHER.addURI(AUTHORITY_ITEMS, "item", ITEMS);
        URI_MATCHER.addURI(AUTHORITY_ITEMS, "item/#", ITEMS_ID);
    }
    

    This way you can decide if only 1 result shall be returned or updated or if all should be queried or not.

    The SQLiteOpenHelper will actually perform the insert and also take care of upgrades if the structure of your SQLite database changes, you can perform it there by overriding

    class ItemDatabaseHelper extends SQLiteOpenHelper {
        public ItemDatabaseHelper(Context context){
            super(context, "myDatabase.db", null, ITEMDATABASE_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String createItemsTable = "create table " + TABLE_ITEMS + " (" +
                ...
            ");";
    
            // Begin Transaction
            db.beginTransaction();
            try{
                // Create Items table
                db.execSQL(createItemsTable);
    
                // Transaction was successful
                db.setTransactionSuccessful();
            } catch(Exception ex) {
                Log.e(this.getClass().getName(), ex.getMessage(), ex);
            } finally {
                // End transaction
                db.endTransaction();
            }
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            String dropItemsTable = "DROP TABLE IF EXISTS " + TABLE_ITEMS;
    
            // Begin transaction
            db.beginTransaction();
    
            try {
                if(oldVersion<2){
                    // Upgrade from version 1 to version 2: DROP the whole table
                    db.execSQL(dropItemsTable);
                    onCreate(db);
                    Log.i(this.getClass().toString(),"Successfully upgraded to Version 2");
                }
                if(oldVersion<3) {
                    // minor change, perform an ALTER query
                    db.execSQL("ALTER ...");
                }
    
                db.setTransactionSuccessful();
            } catch(Exception ex){
                Log.e(this.getClass().getName(), ex.getMessage(), ex);
            } finally {
                // Ends transaction
                // If there was an error, the database won't be altered
                db.endTransaction();
            }
        }
    }
    

    and then the easiest part of all: Perform a query:

    String[] rows = new String[] {"_ID", "_name", "_email" };
    Uri uri = Uri.parse("content://com.yourname.yourapp.Items/item/2";
    
    // Alternatively you can also use getContentResolver().insert/update/query/delete methods
    Cursor c = managedQuery(uri, rows, "someRow=1", null, null); 
    

    That’s basically all and the most elegant way to do it as far as I know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can use -fno-objc-arc flag to disable ARC for files that NOT
I know you can use source control software for source code, but can you
I know I can use Request.Browser.IsMobileDevice . But does anyone know how it works,
Possible Duplicate: Deflate command line tool Yes, I know I can use PHP itself
I know that this is possible in principle, but the tools may not exist
I know that i can use PyObjC to access Cocoa objects in Python. Can
I know I can use the -n flag in git grep to show the
I know I can use install-data-hook to do anything I want after my data
I know I can use the ActionView helper strip_tags method in my views to
I know I can use something like User.sort {|a, b| a.attribute <=> b.attribute} or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.