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

  • SEARCH
  • Home
  • 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 9107637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:37:12+00:00 2026-06-17T02:37:12+00:00

I have two models categories(id, name, slug) places(id, name, cat_id) when i have to

  • 0

I have two models

categories(id, name, slug)
places(id, name, cat_id)

when i have to insert the place details into the database i have a category slug string. So i will need to query the categories id and insert it in to the places category_id

DBAdapter.java

public class DBAdapter {
    // Database and version
    private static final String DATABASE_NAME = "nomad.db";
    private static final int DATABASE_VERSION = 2;

    // Tables
    public static final String TABLE_CATEGORIES = "categories";
    public static final String TABLE_PLACES = "places";
    public static final String TABLE_PHOTOS = "photos";

    // Table Columns
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_SLUG = "slug";
    public static final String COLUMN_ACTIVE = "active";

    public static final String COLUMN_DISTANCE = "distance";
    public static final String COLUMN_DAYS = "days";
    public static final String COLUMN_LAT = "latitude";
    public static final String COLUMN_LNG = "longitude";
    public static final String COLUMN_WEATHER = "weather";
    public static final String COLUMN_TODO = "todo";
    public static final String COLUMN_ABOUT = "about";

    public static final String COLUMN_URL = "url";
    public static final String COLUMN_COPY = "copyright";

    public static final String COLUMN_CAT_ID = "category_id";
    public static final String COLUMN_PLACE_ID = "place_id";

    // Table Queries
    private static final String CREATE_CATEGORIES = "CREATE TABLE categories (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, slug TEXT, active BOOLEAN);";
    private static final String CREATE_PLACES = "CREATE TABLE places (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, slug TEXT, category_id INTEGER, distance TEXT, days TEXT, latitude TEXT, longitude TEXT, weather TEXT, todo TEXT, about TEXT);";
    private static final String CREATE_PHOTOS = "CREATE TABLE photos (_id INTEGER PRIMARY KEY AUTOINCREMENT, place_id INTEGER, url TEXT, copyright TEXT);";

    private final Context context;
    private static DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

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

        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL(CREATE_CATEGORIES);
            database.execSQL(CREATE_PLACES);
            database.execSQL(CREATE_PHOTOS);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w("Nomad", "Upgrading database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS categories");
            db.execSQL("DROP TABLE IF EXISTS places");
            db.execSQL("DROP TABLE IF EXISTS photos");
            onCreate(db);
        }

    }

    public DBAdapter open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        DBHelper.close();
    }

    // Methods

    // Categories

    // Insert Category
    public long insertCategory(String name, String slug) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(COLUMN_NAME, name);
        initialValues.put(COLUMN_SLUG, name);
        initialValues.put(COLUMN_ACTIVE, true);
        return db.insert(TABLE_CATEGORIES, null, initialValues);
    }

    // Get Category ID by Slug
    public int find_city_id(String slug) {
        Cursor c = db.query(TABLE_CATEGORIES, new String[] { COLUMN_ID }, COLUMN_SLUG + "=" + "'" + slug + "'", null, null, null, null, null);
        if (c.moveToFirst()) {
            do{
                return Integer.parseInt(c.getString(0));
            } while (c.moveToNext());
        }
        else
            return -1;
    }


    // Places
    public long insertPlace(String name, String slug, String distance,
            String days, String latitude, String longitude, String weather,
            String todo, String about, int category_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(COLUMN_NAME, name);
        initialValues.put(COLUMN_SLUG, slug);
        initialValues.put(COLUMN_DISTANCE, distance);
        initialValues.put(COLUMN_DAYS, days);
        initialValues.put(COLUMN_LAT, latitude);
        initialValues.put(COLUMN_LNG, longitude);
        initialValues.put(COLUMN_WEATHER, weather);
        initialValues.put(COLUMN_TODO, todo);
        initialValues.put(COLUMN_ABOUT, about);
        return db.insert(TABLE_PLACES, null, initialValues);
    }

}
  • 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-06-17T02:37:14+00:00Added an answer on June 17, 2026 at 2:37 am

    First Create Column level constrains.

     final String CREATE_TABLE_STATE="CREATE TABLE booking_delivery ("
        + "id                INTEGER ,"   
        +"remark             TEXT, "
        +"barcode           INTEGER   REFERENCES customer_info(cust_barcode));";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have two models: store and category with a joining table called categories.stores .
I have two models store and category . A store can have many categories
I have a simple category model: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField()
I have two models : Category and Picture which refers to two tables, Categories
I have two models as follows: icon.rb belongs_to :category attr_accessible :name, :url, :category_id, :icon_for
I have two basic models, Story and Category: class Category(models.Model): title = models.CharField(max_length=50) slug
I have two models User and Category that have a HABTM association. I would
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models, Project and Category, which have a many-to-many relationship between them.

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.