-2

Error :03-26 23:19:58.913 12137-12137/com.example.johnwalls.projet E/SQLiteLog: (1) no such table: email1 03-26 23:19:58.913 12137-12137/com.example.johnwalls.projet E/SQLiteDatabase: Error inserting dem=hdhdhbbdbdjd description=demande source=bechir@gmail.com

inboxFrgment.java

package com.example.johnwalls.projet;


import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import static com.example.johnwalls.projet.R.id.pr;


/**
 * A simple {@link Fragment} subclass.
 */
public class InboxFragment extends Fragment {



    private Button sendB;
    private  EditText d;
    private EditText des;
    private DB base;
    public InboxFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       base=new DB(getActivity());
       final View view = inflater.inflate(R.layout.fragment_inbox, container, false);

        TextView text2 =  view.findViewById(R.id.in);//Find textview Id

        String getArgument = getArguments().getString("main");
        text2.setText(getArgument);


        Button button = (Button) view.findViewById(R.id.log);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {


                String getArgument = getArguments().getString("main");

                EditText d= (EditText) view.findViewById(R.id.dem);
                EditText des=(EditText) view.findViewById(R.id.desc);

                String dem=d.getText().toString();
                String desc=des.getText().toString();


                Email e=new Email();

                e.setSource(getArgument);
                e.setDem(dem);
                e.setDescription(desc);
                base.insertEmail(e);
                Toast.makeText(getActivity(), "Email Envoyé", Toast.LENGTH_SHORT).show();


            }
        });


        return view;
    }

    }

DB.java

package com.example.johnwalls.projet;

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

/**
 * Created by John Walls on 04-Mar-18.
 */

public class DB extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION=1;
    private static final String DB_nom="bef.db";
    private static final String table_admin="admin";
    private static final String table_email1="email1";
    private static final String col_source="source";
    private static final String col_description="description";
    private static final String col_dem="dem";


    private static final String col_cin="cin";
    private static final String col_nom="nom";
    private static final String col_pren="pren";
    private static final String col_email="email";
    private static final String col_password="password";
    private static final String col_age="age";
    SQLiteDatabase db;
    private static final String creation_admin="create table admin (cin integer primary key,"+"nom text not null ,pren text not null ,email text not null,password text not null ,age integer not null);";

    private static final String creation_email="create table email1 (source text primary key,"+"dem text not null ,description text not null);";

    public DB(Context context) {
        super(context,DB_nom, null,DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(creation_admin);
        db.execSQL(creation_email);
        this.db=db;
    }


    public void insertEmail(Email e)
    {
        db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(col_source,e.getSource());
        values.put(col_dem,e.getDem());
        values.put(col_description,e.getDescription());
        db.insert(table_email1,null,values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query ="DROP TABLE IF EXISTS "+table_admin;
        String query2="DROP TABLE IF EXISTS "+table_email1;
        db.execSQL(query);
        db.execSQL(query2);
        this.onCreate(db);
    }
}

What are you trying to achieve what are you expecting to get out What did you get out (include error messages) What else have you tried? What do you think is causing it? Why do you need to make a new question for it? Why is your problem different to other, similar questions on here?

john walls
  • 45
  • 8

1 Answers1

0

I suspect that you have added the email1 table since running the App and that the table has not been created because of the common misconception that the Database helper's onCreate method runs whenever you create an instance of the Database Helper (sub clas of SQLiteOpenHelper i.e. DB.java).

The 'onCreate' method only runs once when the database is created.

You have three options to fix this issue:-

  1. Delete the App's data and then rerun the App.

  2. Uninstall the App and rerun the App.

  3. In you case as the onUpgrade method will drop the tables and then call onCreate, to increase the database version number (e.g. change private static final int DATABASE_VERSION=1; to private static final int DATABASE_VERSION=2;) and then rerun the App.

MikeT
  • 42,310
  • 13
  • 44
  • 61