Code example for SQLiteDatabase

Methods: execSQLinsertquery

0
			m_db = myCtx.openOrCreateDatabase(DATABASE_NAME, 0,null);
 
			// Check for the existence of the DBVERSION table 
			// If it doesn't exist than create the overall data, 
			// otherwise double check the version 
			Cursor c =
				m_db.query("sqlite_master", new String[] { "name" },
						"type='table' and name='"+TABLE_DBVERSION+"'", null, null, null, null);
			int numRows = c.getCount();
			if (numRows < 1)
			{ 
				CreateDatabase(m_db);
			} 
			else 
			{ 
				int version=0;
				Cursor vc = m_db.query(true, TABLE_DBVERSION, new String[] {"version"},
						null, null, null, null, null,null); 
				if(vc.getCount() > 0) {
				    vc.moveToLast();
				    version=vc.getInt(0);
				} 
				vc.close();
				if (version!=DATABASE_VERSION)
				{ 
					Log.e(TAG,"database version mismatch");
					MigrateDatabase(m_db, version, DATABASE_VERSION);
//					deleteDatabase(); 
//					CreateDatabase(db); 
//					populateDatabase(); 
				} 
			} 
			c.close();
			 
 
		} 
		catch (SQLException e)
		{ 
			Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
		} 
   } 
 
   private void CreateDatabase(SQLiteDatabase db)
   { 
		try 
		{ 
			db.execSQL(DBVERSION_CREATE);
			ContentValues args = new ContentValues();
			args.put("version", DATABASE_VERSION);
			db.insert(TABLE_DBVERSION, null, args);
 
			db.execSQL(TABLE_CREATE);
		} 
		catch (SQLException e)
		{ 
			Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
		}  
   } 
    
   private void MigrateDatabase(SQLiteDatabase db, int fromVersion, int toVersion)
   { 
		try 
		{ 
			if ((fromVersion == 1)&&(toVersion == 2))
			{ 
				db.execSQL(TABLE_MIGRATE_1_2);
				ContentValues args = new ContentValues();
				args.put("version", DATABASE_VERSION);
				db.insert(TABLE_DBVERSION, null, args);
			} 
		} 
		catch (SQLException e)
		{ 
			Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());