如何连接两个表以返回用户的注释? [英] How do I connect the two tables to return the notes for the user?

查看:80
本文介绍了如何连接两个表以返回用户的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在android中的第一个项目,我一直在努力寻找查询并连接两个表格,我希望得到用户信息以及说明..







这里是代码..



This is my first project in android and i have been struggling for the query and connecting the two tables and i want to get the user information along with the note..



here is the code..

public class NotebookDbAdapter {

    private static final String DATABASE_NAME = "notebook.db";
    private static final int DATABASE_VERSION = 3;


    public static final String USER_TABLE = "users";
    public static final String COLUMN_ID_USER = "_id";
    public static final String COLUMN_EMAIL = "email";
    public static final String COLUMN_PASS = "password";

    public static final String NOTE_TABLE = "note";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_MESSAGE = "message";
    public static final String COLUMN_CATEGORY = "category";
    public static final String COLUMN_DATE = "date";

    private String[] allColumns = {COLUMN_ID, COLUMN_TITLE, COLUMN_MESSAGE, COLUMN_CATEGORY,
            COLUMN_DATE};

    public static final String CREATE_TABLE_NOTE = " create table " + NOTE_TABLE + " ( "
            + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_TITLE + " text not null, "
            + COLUMN_MESSAGE + " text not null, "
            + COLUMN_CATEGORY + " text not null, "
            + COLUMN_DATE + ");";

    public static final String CREATE_TABLE_USERS = " create table " + USER_TABLE + " ( "
            + COLUMN_ID_USER + " integer primary key autoincrement, "
            + COLUMN_EMAIL + " TEXT , "
            + COLUMN_PASS + " TEXT );";

    private SQLiteDatabase sqlDB;
    private Context context;

    private NotebookDbHelper notebookDbHelper;

    public NotebookDbAdapter(Context ctx) {
        context = ctx;
    }

    public NotebookDbAdapter open() throws android.database.SQLException {
        notebookDbHelper = new NotebookDbHelper(context);
        sqlDB = notebookDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        notebookDbHelper.close();
    }

    public Note createNote(String title, String message, Note.Category category) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_TITLE, title);
        values.put(COLUMN_MESSAGE, message);
        values.put(COLUMN_CATEGORY, category.name());
        values.put(COLUMN_DATE, Calendar.getInstance().getTimeInMillis() + "");

        long insertId = sqlDB.insert(NOTE_TABLE, null, values);

        Cursor cursor = sqlDB.query(NOTE_TABLE,
                allColumns, COLUMN_ID + " = " + insertId, null, null, null, null);

        cursor.moveToFirst();
        Note newNote = cursorToNote(cursor);
        cursor.close();
        return newNote;
    }

    public long deleteNote(long idToDelete) {
        return sqlDB.delete(NOTE_TABLE, COLUMN_ID + " = " + idToDelete, null);
    }

    public long updateNote(long idToUpdate, String newTitle, String newMessage, Note.Category newCategory) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_TITLE, newTitle);
        values.put(COLUMN_MESSAGE, newMessage);
        values.put(COLUMN_CATEGORY, newCategory.name());
        values.put(COLUMN_DATE, Calendar.getInstance().getTimeInMillis() + "");

        return sqlDB.update(NOTE_TABLE, values, COLUMN_ID + " = " + idToUpdate, null);

    }





我的尝试:



我一直在尝试,但仍然没有得到任何错误......有人可以指导正确的道路让我的项目成功吗?谢谢!



What I have tried:

I been trying it and still get nothing and get errors... can someone guide to the right path for making my project successful? thank you!

推荐答案

似乎在NOTE_TABLE的create语句中你缺少COLUMN_DATE的数据类型。所以你可能应该有类似的东西



It seems that in the create statement of NOTE_TABLE you're missing the data type for COLUMN_DATE. So you probably should have something like

public static final String CREATE_TABLE_NOTE = " create table " + NOTE_TABLE + " ( "
        + COLUMN_ID + " integer primary key autoincrement, "
        + COLUMN_TITLE + " text not null, "
        + COLUMN_MESSAGE + " text not null, "
        + COLUMN_CATEGORY + " text not null, "
        + COLUMN_DATE + " integer);";







有关日期的更多信息在SQLite中,请参阅 SQLite版本3中的数据类型 [ ^ ]


这篇关于如何连接两个表以返回用户的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆