0
    package com.example.mohamed.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by mohamed on 5/22/2016.
 */
public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DB_VERSION=2;
    private static final String DB_NAME="products.db";
    public static final String TABLE_PRODUCTS="products";
    public static final String Column_id="_id";
    public static final String Column_Products="productname";

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DB_NAME, factory, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                Column_id +" INTEGER PRIMARY KEY AUTOINCREMENT ," +
                Column_Products + " TEXT " +
                ");";
        db.execSQL(query);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }

    public void addproduct (Products product){

        ContentValues values=new ContentValues();
        values.put(Column_Products,product.get_productname());
        SQLiteDatabase db=getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();

    }
    public void deleteproduct (String productname)
    {

        SQLiteDatabase db=getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS +"WHERE" +Column_Products + "=\"" + productname + "\";");

    }

    public String DatabaeToString(){
        String dbstring ="";
        SQLiteDatabase db=getWritableDatabase();
        String query ="SELECT * FROM " + TABLE_PRODUCTS +" WHERE 1";

        Cursor c = db.rawQuery(query,null);
        c.moveToFirst();

        while (!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("productname"))!=null){
            dbstring +=c.getColumnIndex("productname");
            dbstring +="\n" ;
            }
        }
        return dbstring;

    }
}

[Logcat scrshot][2] **i have worked one day on fixing this code table products has no column named productname **android.database.sqlite.SQLiteException: table products has no column named productname (code 1): , while compiling: INSERT INTO products(productname) VALUES (?)

Logcat screen shot

Mohamed Farahat
  • 751
  • 1
  • 7
  • 12

1 Answers1

0

You have the source code of this tutorial on this site: https://thenewboston.com/forum/topic.php?id=3767

Nenco
  • 241
  • 4
  • 13