I try to run this app wich create a DB. It has 3 classes one for create the arraylist, another where you can put the text for the DB and the last just create the SQLite. The first one is
public class TestDatabaseActivity extends ListActivity{
private CommentsDataSource datasource;
private int forresult=1;
private String name,descrip,group;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
the second
public class CommentsDataSource {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_COMMENT,
MySQLiteHelper.COLUMN_COMPONENTS };
public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Comment createComment(String[] comment) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, comment[0]);
values.put(MySQLiteHelper.COLUMN_COMMENT, comment[1]);
values.put(MySQLiteHelper.COLUMN_COMPONENTS, comment[2]);
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, MySQLiteHelper.COLUMN_ID + " =" + insertId, null,
null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}
public void deleteComment(Comment comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return comments;
}
and the last which create the SQLite
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_COMMENT = "description";
public static final String COLUMN_COMPONENTS = "players";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, "+
COLUMN_NAME + " text not null,"+
COLUMN_COMMENT + " text not null, "+
COLUMN_COMPONENTS + " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
All of this give me the error java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ownevent/com.example.ownevent.TestDatabaseActivity}: android.database.sqlite.SQLiteException: no such column: name (code 1): , while compiling: SELECT _id, name, description, players FROM comments.
As if i don't create properly the name Column. I think this is good.
Aucun commentaire:
Enregistrer un commentaire