2

I have some code in my application, but when I insert a table I get an error. I don't know why. Can any one help me??

This is my code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tambahpenerima);

    data = new dbHelper(this);
    db = data.getWritableDatabase();
    data.createTable(db);

    na = (TextView)findViewById(R.id.na);
    no = (TextView)findViewById(R.id.no);
    pesanx = (EditText)findViewById(R.id.hasilx);

    Bundle paket = getIntent().getExtras();
    nama = paket.getString("nama");
    nomor = paket.getString("nomor");
    hasil = paket.getString("chiper");
    na.setText(nama);
    no.setText(nomor);
    pesanx.setText(hasil);

    pesanKeluar objk = new pesanKeluar(nama, nomor, hasil);
    inputDataK(db, objk);   //here the error    
}
public void inputDataK(SQLiteDatabase db, pesanKeluar k) {
    ContentValues cv = new ContentValues();
    cv.put("nama", k.getNama());
    cv.put("nomortlp", k.getNomorTlp()); //here my modified
    cv.put("chiperteks", k.getChiperteks()); //here my modified
    db.insert("pesanKeluar", null, cv); //here my modified
    db.close();
}
}

I've modified my code, but I got the same error as before. Please take a look at my log error.

    03-11 17:27:33.704: I/Database(1664): sqlite returned: error code = 
1, msg = table pesanKeluar has no column named chiper
    03-11 17:27:33.725: E/Database(1664): Error inserting 
chiper=00010101 nama=D nomor=536
    03-11 17:27:33.725: E/Database(1664): 
android.database.sqlite.SQLiteException: table pesanKeluar has no column
 named chiper: , while compiling: INSERT INTO pesanKeluar(chiper, nama, 
nomor) VALUES(?, ?, ?);

Here is my dbHelper.java:

package com.databasesms;

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

public class dbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "dbenkripsisms.db";

public dbHelper ( Context context) {
    super ( context, DATABASE_NAME, null, 1);
}

public void createTable ( SQLiteDatabase db) {
    db.execSQL("CREATE TABLE if not exists tbPesan (id INTEGER PRIMARY KEY AUTOINCREMENT, plainteks TEXT, key TEXT, chiperteks TEXT);");
    db.execSQL("CREATE TABLE if not exists pesanMasuk (id_pm INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, nomortlp NUMBER, plainteks TEXT, key TEXT);");
    db.execSQL("CREATE TABLE if not exists pesanKeluar (id_pk INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, nomortlp TEXT, chiperteks TEXT);");
}

@Override
public void onCreate ( SQLiteDatabase arg0 ) {

}

@Override
public void onUpgrade ( SQLiteDatabase arg0, int arg1, int arg2 ) {

}


} 

Here is my pesanKeluar.java:

package com.databasesms;

public class pesanKeluar {
private String nama;
private String nomortlp;
private String chiperteks;

public pesanKeluar () {
    super();
}

public pesanKeluar ( String nama, String nomortlp, String chiperteks ) {
    this.nama = nama;
    this.nomortlp = nomortlp;
    this.chiperteks = chiperteks;       
}

public String getNama () {
    return nama;
}

public void setNama ( String nama ) {
    this.nama = nama;
}

public String getNomorTlp () {
    return nomortlp;
}

public void setNomorTlp ( String nomortlp ) {
    this.nomortlp = nomortlp;
}

public String getChiperteks () {
    return chiperteks;
}

public void setChiperteks ( String chiperteks ) {
    this.chiperteks = chiperteks;
}

}

7 Answers 7

1

Try the above said solutions like replacing the column name and after that just clear your data before relaunching the application.

Sign up to request clarification or add additional context in comments.

2 Comments

how can i clear my data? i dont know where is my database location. i've search on file explore but i dont find.
Go to settings>>apps>>select your app name>> in detail you can have an option to clear data.
0

“has no column” error clearly states that, the column is not present the table you specified. So either you are referring to wrong table or you have mis-spelled your column name. In your case you have mis-spelled the column name. While creating table you have named fourth column as chiperteks and while inserting data into this column you are refering it as chiper which is totally wrong.

Change is as below, then it will start working,

public void inputDataK(SQLiteDatabase db, pesanKeluar k) {
    ContentValues cv = new ContentValues();
    cv.put("nama", k.getNama());
    cv.put("nomor", k.getNomorTlp());
    cv.put("chiperteks", k.getChiperteks());  // Change here
    db.insert("pesanKeluar", "nama", cv); //here the error
    db.close();
}

2 Comments

i've tried that to, but i got the same error like before. i really dont know why. can u help me?
@niaa, i saw, but i think this line hasil = paket.getString("chiper"); also need same change.
0

You have given the coulmn name chiperteks not chiper db.execSQL("CREATE TABLE if not exists pesanKeluar (id_pk INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, nomortlp TEXT, chiperteks TEXT);");

Comments

0

table pesanKeluar has no column named chiper

The error says it all your table pesanKeluar has a column name chiperteks... and you are giving a key "chiper" to insert.

Comments

0

Error inserting chiper=00010101 nama=D nomor=536 03-11 17:27:33.725: E/Database(1664): android.database.sqlite.SQLiteException: table pesanKeluar has no column named chiper: , while compiling: INSERT INTO pesanKeluar(chiper, nama, nomor) VALUES(?, ?, ?);

Its occured when column in your function is not same as in database, check your database whether chiper named colume is exist or not or its same spell as you have in your database?

you defines column name as chiperteks while creating table and when inserting data you are passing wrong as chiper, thats cause error.

Use:

cv.put("chiperteks", k.getChiperteks());

Instead:

cv.put("chiper", k.getChiperteks());

2 Comments

I have changed chiper to chiperteks but i got the same error. any solution more?
then cross check whether table is create or not? and compare your table columns with your insertion function.
0

you have the column name chiperteks but not chiper in your table pesanKeluar

replace

cv.put("chiper", k.getChiperteks());

to

cv.put("chiperteks", k.getChiperteks());

Comments

0

Try this

public void inputDataK(SQLiteDatabase db, pesanKeluar k) {
        ContentValues cv = new ContentValues();
        cv.put("nama", k.getNama());
        cv.put("nomortlp ", k.getNomorTlp()); // column name should be same as you define in Table
        cv.put("chiperteks ", k.getChiperteks());
        db.insert("pesanKeluar ", null, cv); 
        db.close();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.