Dbsqlite Android Studio Problem

Asked

Viewed 62 times

-4

Good afternoon, I have syntax problem in my Sqlite code in Android Studio. Could someone help me?

package.com.projetofinal;

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

import java.util.ArrayList;
import java.util.List;

public class DBcadastro {

    private static final String DATABASE_NAME = "bancodedados.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "cadastro";

    private Context context;
    private SQLiteDatabase db;

    private SQLiteStatement InsertStnt;
    private static final String Insert = "insert into " + TABLE_NAME + " (nome, cpf, idade, telefone, email) values (?, ?, ?, ?, ?)";


    public DBcadastro(Context context){
        this.context = context;
        Opencadastro opencadastro = new Opencadastro(this.context);
        this.db = opencadastro.getWritableDatabase();
        this.InsertStnt = this.db.compileStatement(Insert);
    }

    public long insert (String nome, int cpf,int idade, int telefone, String email ){
        this.InsertStnt.bindString(1,nome);
        this.InsertStnt.bindLong(2,cpf);
        this.InsertStnt.bindLong(3,idade);
        this.InsertStnt.bindLong(4,telefone);
        this.InsertStnt.bindString(5,email);
        return this.InsertStnt.executeInsert();
    }

    public void deleteAll (){
        this.db.delete(TABLE_NAME,null, null);
    }

    public List<cadastro> queryGetALL(){
        List<cadastro> list = new ArrayList<cadastro>();

        try {

            Cursor cursor = this.db.query (TABLE_NAME, new String[] {"nome", "cpf","idade", "telefone", "email"},
                    null, null, null, null, null);

            int registros = cursor.getCount();

            if (registros != 0){
                cursor.moveToFirst();

                do {
                    cadastro cadastro = new cadastro(cursor.getString(0), cursor.getInt(1), cursor.getInt(2),cursor.getInt(3),cursor.getString(4));
                    list.add(cadastro);
                } while (cursor.moveToNext());

                if (cursor  != null && ! cursor.isClosed())
                    cursor.close();
                    return list;
            }else
                return null;

        }

        catch (Exception err) {
            return null;
        }
    }

    private static class Opencadastro extends SQLiteOpenHelper{
        Opencadastro(Context context){
            super(context, TABLE_NAME, null, DATABASE_VERSION);

        }

        public void onCreate (SQLiteDatabase db){
            String sql = "CREATE TABLE IP NOT EXISTS " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT " +
                    "cpf INT, idade INT, telefone INT, email TEXT);";
            db.execSQL(sql);
        }

        public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

}
  • 3

    Ana, you’d better edit your question by adding Exception as text instead of pasting images. If your image link expires sometime, your question will be incomplete for future users.

1 answer

1

You have a syntax error in SQL that creates the table.

The correct syntax is IF NOT EXISTS and not NOT EXISTS

Instead of CREATE TABLE IP NOT EXISTS use CREATE TABLE IP IF NOT EXISTS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.